Is it possible to use reduce to find the second largest element in an array? Without using sort methods. Something like this:
Obs: The code below is for finding the largest value. I need to find the second largest value and would like to use reduce(). Without using the sort methods
array1 = [12, 16, 1, 5]
array2 = [8, 4, 5, 6];
function largestElement(array){
largestE = array.reduce((acc,currentValue) => currentValue > acc ? currentValue : acc )
return largestE
}
console.log(largestElement(array1))
console.log(largestElement(array2))
WITHOUT DOING THIS:
function secondLargest(array){
array.sort((a,b) => a-b)
return array[array.length-2]
}