Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; sort the array ; start from second last element as the largest element is at last ; if the element is not equal to largest element ; Driver program to test above function
function print2largest ( arr , arr_size ) { let i , first , second ; if ( arr_size < 2 ) { document . write ( " " ) ; return ; } arr . sort ( ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { document . write ( " " + arr [ i ] ) ; return ; } } document . write ( " " ) ; } let arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] ; let n = arr . length ; print2largest ( arr , n ) ;