varsecondMax=function(){var arr =[20,120,111,215,54,78];// use int arraysvar max =Math.max.apply(null, arr);// get the max of the array
arr.splice(arr.indexOf(max),1);// remove max from the arrayreturnMath.max.apply(null, arr);// get the 2nd max};
javascript find the second highest Element from array
varsecondMax=function(){var arr =[20,120,111,215,54,78];// use int arraysvar max =Math.max.apply(null, arr);// get the max of the array
arr.splice(arr.indexOf(max),1);// remove max from the arrayreturnMath.max.apply(null, arr);// get the 2nd max};
varsecondMax=function(arr){var max =Math.max.apply(null, arr),// get the max of the array
maxi = arr.indexOf(max);
arr[maxi]=-Infinity;// replace max in the array with -infinityvar secondMax =Math.max.apply(null, arr);// get the new max
arr[maxi]= max;return secondMax;};
How would you find the second largest number in an array?
// Java program to find second largest// element in an arrayimport java.util.*;classGFG{// Function to print the// second largest elementsstaticvoidprint2largest(int arr[],
int arr_size){
int i, first, second;// There should be// atleast two elementsif(arr_size <2){System.out.printf(" Invalid Input ");return;}// Sort the arrayArrays.sort(arr);// Start from second last element// as the largest element is at lastfor(i = arr_size -2; i >=0; i--){// If the element is not// equal to largest elementif(arr[i]!= arr[arr_size -1]){System.out.printf("The second largest "+
"element is %d
", arr[i]);return;}}System.out.printf("There is no second "+
"largest element
");}// Driver codepublicstaticvoidmain(String[] args){
int arr[]={12,35,1,10,34,1};
int n = arr.length;print2largest(arr, n);}}// This code is contributed by gauravrajput1