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

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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