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

339
Views
How to compare two strings and delete diffencies- Make anagrams

I know this is topic which is already here on stackoverflow, but these approaches are the ones I do not want to follow as I do not understand them, so it's wasting of time to just copy it.

I am pretty new in Javascript and I would like to ask you for help with anagrams. I am working on tasks from HackerRank and there is one with anagram.

Currently I have created few approaches which I think can lead to correct result, but it's not finished and I feel lost after hours of searching for good looking solution.

Here is solution 1: I think this would work, but there is an issue with counting, it will return me 1 as only letter "C" is in the both strings, but what I want to return 2 as there were two "C" letters. Is there a way how I can achieve it ?

function makeAnagram(a, b) {
let count  =  0;
arrayA = Array.from(a);
arrayB = Array.from(b);
const try3 = try1.concat(try2);   
            let sameLetters = arrayA.filter(item => arrayB.includes(item))
            {
                count++;
            }
            console.log(sameLetters);
            console.log(count);
            
    count =  (try3.length - sameLetters2.length);
}
console.log(makeAnagram("cde", "abc"));

The next solution was with for cycles, but probably this is not enough and not correct

function makeAnagram(a, b) {
let count = 0;
let deleted = 0;  
for(let i = 0; i < a.length; i++)
{
    for(let j = 1; j < b.length; j++)
    {
        if(arrayA[i] == arrayB[i])
        {
            count++;
        }
        else
        {
            deleted++;
        }
    }
}

And the last solution I was trying and seems to me like a good solution is:

function makeAnagram(a, b) {
const array2 = [...a.split(""), ...b.split("")]
console.log(array2);
const something = array2.reduce((acc, letter) => array2.includes(letter) ? [] : [...acc, letter],[])
    }
}

Thank you

about 4 years ago · Juan Pablo Isaza
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!