Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; finding the largest element ; Now find the second largest element ; Driver program to test above function
function print2largest ( arr , arr_size ) { let i ; if ( arr_size < 2 ) { document . write ( " " ) ; return ; } let largest = second = - 2454635434 ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > largest ) { largest = arr [ i ] ; } } for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > second && arr [ i ] < largest ) { second = arr [ i ] ; } } if ( second == - 2454635434 ) { document . write ( " " ) ; } else { document . write ( " " + second ) ; return ; } } let arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] ; let n = arr . length ; print2largest ( arr , n ) ;