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

171
Vistas
Find and Calculate Mode in Javascript

I am having some difficulity in how to actually get mode and count for the biggest value of the map. I can't get to count for mode because it show for multiplication 2, what i think happens in my code, is the matter of the loop. Thanks in advance.

var data1 = [1, 1, 1, 2, 2, 4, 1, 1]
function getMode (arr) {
    var obj = {};
    var max = 0;
    let result = []


    arr.forEach(element => {
        if (!obj[element]) obj[element] = 0
        obj[element]++
    });
    if (Object.keys(obj).length === result.length) {
        result = []
    }

    for (let i = 0; i < arr.length; i++) {
        obj[arr[i]] = (obj[arr[i]] + 1) || 1;
    }
    for (var key in obj) {
        if (obj[key] > max) {
            result = [key]
            max = obj[key]
        } else if (obj[key] === max) {
            result.push(key)
        }
    }
    return ("most number value " + result + " with " + max)
}

console.log(getMode(data1));
about 4 years ago · Juan Pablo Isaza
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