Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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