Skip to content

Instantly share code, notes, and snippets.

@Neptune998
Created August 8, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neptune998/5f7395d83d147114b4cdeae76b38b010 to your computer and use it in GitHub Desktop.
Save Neptune998/5f7395d83d147114b4cdeae76b38b010 to your computer and use it in GitHub Desktop.
function getSecondLargest(arr) {
let max = -Infinity, result = -Infinity;
for (const value of arr) {
const next_ele = Number(value)
if (next_ele > max) {
[result, max] = [max, next_ele] // save previous max
}
else if (next_ele < max && next_ele > result)
{
result = next_ele; // new second biggest
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment