Created
February 22, 2017 22:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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