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

172
Visualizações
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 à 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