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

324
Visualizações
JavaScript Trying to Print The Number of Times a Letter Appears In String But It Prints More than Once

In the code below, I am trying to check how many times a letter in a string appears. Problem with the code below is that it prints each letter more than once. It needs to collect all the same letters and show the number of times it occurs in the string and display it once.

const string = 'mississippi'

const letters = [...string]

let currentLetter = ''
let letterOccurance = []


for(let i = 0; i < letters.length; i++){
  let letterFrequency = letters.filter((letter)=>{
    return letter === letters[i]
  })

  letterOccurance.push([`${letters[i]}`,letterFrequency.length])
}

console.log(letterOccurance)

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

That's too much code just to get the number of times a letter appears in a string. Try the following code:

const string = 'mississippi';

let frequency = {};

for (let letter of string) {
  if (frequency[letter]) {
    frequency[letter]++;
  } else {
    frequency[letter] = 1;
  }
}

console.log(frequency);

about 4 years ago · Santiago Trujillo Relatório

0

You're always pushing the letter to the array, whether it already exists there or not:

letterOccurance.push([`${letters[i]}`,letterFrequency.length])

You could check if it exists first:

if (!letterOccurance.find(l => l[0] === letters[i])) {
  letterOccurance.push([`${letters[i]}`,letterFrequency.length])
}

Or even skip it entirely if you've already seen it, since the first time you find any letter you already know its complete count:

for(let i = 0; i < letters.length; i++){
  if (letterOccurance.find(l => l[0] === letters[i])) {
    continue;
  }
  // the rest of the loop...
}

There's honestly a variety of ways you could step back and re-approach the problem. But for the question about why letters are repeating, that's simply because each iteration of the loop unconditionally appends the letter to the resulting array.

about 4 years ago · Santiago Trujillo Relatório

0

How about writing a more generic item-counting function and then layering countLetters as a simple partial application of the identity function?

const countBy = (fn) => ([...xs]) =>
  xs .reduce ((a, x) => {const k = fn (x); a [k] = (a[k] || 0) + 1; return a}, {})

const countLetters = countBy (x => x)

console .log (countLetters ('missisippi'))

countBy is fairly generic. You pass it a function to convert your values to strings, and pass your array of items to the function it returns. Strings are array-like enough that this just works for our simple countLetters. But we could use it for other counts as well, such as:

countBy (x => x .grade) ([{id: 1, grade: 'A'}, {id: 2, grade: 'B'}, {id: 3, grade: 'A'}])
//=> {"A": 2, "B": 1}
about 4 years ago · Santiago Trujillo 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