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

143
Visualizações
Finding the Most Recurring Character
function maxRecurringChar(text) {
  let charMap = {};
  let maxCharValue = 0;
  let maxChar = '';
  for (let char of text) {
    if (charMap.hasOwnProperty(char)) {
      charMap[char]++;
    } else {
      charMap[char] = 1;
    }
  }
  for (let char in charMap) {
    if (charMap[char] > maxCharValue) {
      maxCharValue = charMap[char];
      maxChar = char;
    }
  }
  return maxChar;
}

Here is the code I don't understand, cause first for always will return 1 , if always will return 1 why do we need to write the first loop?

Here is the course link

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

0

The algorithm creates a new Object charMap to which all characters of the String text will be added as attributes. In the first for-loop you either add a new attribute with the name of the current char and set the counted value to 1 or, if already present, increment the counted value if the char was already found in the string. So you effectivly count for every char the number of occurences.

In the second for-loop the Object is searched for the greatest counted value, which will be the char with the highest count of occurences.

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