Skip to content

Instantly share code, notes, and snippets.

@RachelSa
Created July 7, 2016 22:57
Show Gist options
  • Save RachelSa/a3cc2ca7fd66323c14ba74782df43832 to your computer and use it in GitHub Desktop.
Save RachelSa/a3cc2ca7fd66323c14ba74782df43832 to your computer and use it in GitHub Desktop.
removes n number of elements from an array, largest to smallest
//remove n number of elements from an array, largest to smallest
function removeLargest(arr,n){
for (var i = 0; i < n; i ++){
var sorted = Math.max.apply(null, arr);
//run through array, funding largest
var rem = arr.splice(arr.indexOf(sorted),1);
//remove largest
} console.log(arr);
}
removeLargest([1,5,3,6,4],2)
removeLargest([-1,-2,3,4,-5,6],3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment