Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

353
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda