I wrote this code to get second max...it worked in cases like nums=[1,2,3,2,4] and i got second max equal to 3.But if mu array is [1,2,3,4,5,6,7,8,9,10] the output for second max is 8. Please help.
function getSecondLargest(arr){
let uniqueArr = [ ...new Set(arr) ];
uniqueArr.sort();
const z= uniqueArr.length;
return arr[z-2];
}