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

186
Vistas
find max elements of an array in JS, elements might be same

I know there are a lot of questions about how to get max from an array, but they all have unique elements. my scenario is different, for example:

find_multiple_max([10,20,30,30])

will return

[30, 30]

I found a solution and it works:

function find_multiple_max(arr) {
    var max;
    arr = arr.sort((a,b) => b-a)
            .filter(e => {
                if (!max) {
                    max = e;
                    return true;
                } 
                return (e == max);
            });
    console.log(arr);
    return arr;
}

I was still wondering if any simpler answer exists.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can still use Math.max which will return the maximum element from the array and then use filter to get an array

function find_multiple_max(arr) {
  let maxElem = Math.max(...arr);
  return arr.filter(item => item == maxElem)
}
console.log(find_multiple_max([10, 20, 30, 30]))

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