Skip to content

Instantly share code, notes, and snippets.

@loschtreality
Created February 22, 2017 22:32
Show Gist options
  • Save loschtreality/1b1f0b3e8d1e9e65a0c5209ff97568c2 to your computer and use it in GitHub Desktop.
Save loschtreality/1b1f0b3e8d1e9e65a0c5209ff97568c2 to your computer and use it in GitHub Desktop.
Find fifth largest element in the array. Don't use any native sort methods
const fifth_largest = (array) => {
const cache = []
let highest = -Infinity
let highest_index = -1
for (var i = 0; i < 5; i++) {
for (var index = 0; index < array.length; index++) {
if (array[index] > highest) {
highest = array[index]
highest_index = index
}
}
cache.push(highest)
array[highest_index] = -Infinity
highest = -Infinity
highest_index = -1
}
return cache[cache.length -1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment