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

139
Visualizações
common character count challenge on Code signal

When I loop through both arrays I am getting a pass on most of the tests. One of the tests creates a problem as continues the nest loop against the first loop even when the matched element is removed.

Input: s1: "abca" s2: "xyzbac"

Heres my code:

function commonCharacterCount(s1, s2) {
    const arrayOne = s1.split("")
    const arrayTwo = s2.split("")
    var matches = [];

    for (let i = 0; i < arrayOne.length; i++) {
        for (let j = 0; j < arrayTwo.length; j++) {
            console.log(arrayTwo[j],arrayOne[i], matches)
           if (arrayOne[i] === arrayTwo[j]) {
               matches.push(arrayOne[i])
               arrayOne.splice(arrayOne[i], 1)
        
           }
        }
    }
    return matches.length
}

I checked the console log on test 3 which is the only one that is failing and I can see there is an issue skipping over the second item "b".

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you are slicing arrayOne you remove an item from arrayOne and this means that it will skip over an item because the variable i gets incremented by 1 but the arrayOne loses an item. So you get something like this.

arrayOne = ["a","b","c","d"]  
i = 0  
arrayOne[i] results to "a"

now you find a match with arrayTwo and you slice arrayOne at the index i of your match so arrayOne becomes

arrayOne = ["b","c","d"]  
//however you still increment i by one so i becomes  
i = 1  
//so now arrayOne[i] becomes  
arrayOne[i] results to "c"

Now I'm not sure my explanation is any good but I have two solutions. 1 doesn't splice and the other does but when it does it decrements i by one.

option 1

function commonCharacterCount(s1, s2) {
    const arrayOne = s1.split("")
    const arrayTwo = s2.split("")
    var matches = [];

    for (let i = 0; i < arrayOne.length; i++) {
        for (let j = 0; j < arrayTwo.length; j++) {
            console.log(arrayTwo[j],arrayOne[i], matches)
           if (arrayOne[i] === arrayTwo[j]) {
               matches.push(arrayOne[i])        
           }
        }
    }
    return matches.length
}

option 2

function commonCharacterCount(s1, s2) {
    const arrayOne = s1.split("")
    const arrayTwo = s2.split("")
    var matches = [];

    for (let i = 0; i < arrayOne.length; i++) {
        for (let j = 0; j < arrayTwo.length; j++) {
            console.log(arrayTwo[j],arrayOne[i], matches)
           if (arrayOne[i] === arrayTwo[j]) {
               matches.push(arrayOne[i])
               arrayOne.splice(arrayOne[i], 1)
               i--
           }
        }
    }
    return matches.length
}
about 4 years ago · Juan Pablo Isaza Relatório
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