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 code
def print2largest ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 2 ) : NEW_LINE INDENT print ( " ▁ Invalid ▁ Input ▁ " ) NEW_LINE return NEW_LINE DEDENT arr . sort NEW_LINE for i in range ( arr_size - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ arr_size - 1 ] ) : NEW_LINE print ( " The ▁ second ▁ largest ▁ element ▁ is " , arr [ i ] ) NEW_LINE return NEW_LINE DEDENT print ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element " ) NEW_LINE DEDENT arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print2largest ( arr , n ) NEW_LINE