Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!