Find Second largest element in an array | C # program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; Find the largest element ; Find the second largest element ; Driver code
using System ; class GFG { static void print2largest ( int [ ] arr , int arr_size ) { int i , second ; if ( arr_size < 2 ) { Console . Write ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int largest = second = int . MinValue ; for ( i = 0 ; i < arr_size ; i ++ ) { largest = Math . Max ( largest , arr [ i ] ) ; } for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] != largest ) second = Math . Max ( second , arr [ i ] ) ; } if ( second == int . MinValue ) Console . Write ( " There ▁ is ▁ no ▁ second ▁ " + " largest ▁ element STRNEWLINE " ) ; else Console . Write ( " The ▁ second ▁ largest ▁ " + " element ▁ is ▁ { 0 } STRNEWLINE " , second ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . Length ; print2largest ( arr , n ) ; } }