Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

347
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda