How to find the k biggest values from an array in JavaScript

1 Answer

0 votes
function pickMaxK(arr, k) {
  return [...arr].sort((a, b) => b - a).slice(0, k);
}

const arr = [11, 2, 4, 9, 3, 6, 5, 1];
const k = 3;

console.log(pickMaxK(arr, k)); 


/*
run:
 
[ 11, 9, 6 ]
 
*/

 



answered Apr 6 by avibootz
...