Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How to Know if array has same letters as string

so what I have been trying to acheive is that if I iterate over arr and string and if they have the same letters then some action should be perfomed, for example - if ("both string and array have the same letters"){ "some action" }

const word = "hello";

const arr = ["o", "l", "e", "h"];
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

const word = "hello";
const arr = ["o", "l", "e", "h"];

const uWord = [...new Set(word)].sort().join()
const uArr = [...new Set(arr)].sort().join()

if (uWord === uArr) {
  // do something
}
about 4 years ago · Santiago Trujillo Report

0

Here is with Linear time O(n). Check whether both items has same chars and same chars count.

const isMatch = (arr, word, track = {}) => {
  arr.forEach((char) => (track[char] = (track[char] ?? 0) + 1));
  [...word].forEach((char) => (track[char] = (track[char] ?? 0) - 1));
  return !Object.values(track).some((val) => val < 0);
};


console.log(isMatch(["o", "l", "e", "h"], "hello"));
console.log(isMatch(["o", "l", "e", "h", "l"], "hello"));
console.log(isMatch(["o", "o"], "lo"));

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!