let getTheSecondlargest = function (arr) {
let max = -Infinity, secondmax = -Infinity;
for (const n of arr) {
if (n > max) {
[secondmax, max] = [max, n]
} else if (n < max && n > secondmax) {
secondmax = n;
}
}
return secondmax;
}
const arr = [34, 3, 8, 2, 9, 4, 6];
console.log(getTheSecondlargest(arr));
const arr1 = [1, 1, 2, 1, 1];
console.log(getTheSecondlargest(arr1));
const arr2 = [5, 5, 5, 5, 5, 5, 5];
console.log(getTheSecondlargest(arr2));
/*
run:
9
1
-Infinity
*/