Created
August 8, 2020 12:16
-
-
Save Neptune998/5f7395d83d147114b4cdeae76b38b010 to your computer and use it in GitHub Desktop.
This file contains 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
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