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 ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } sort ( arr , arr + arr_size ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { printf ( " The ▁ second ▁ largest ▁ element ▁ is ▁ % d STRNEWLINE " , arr [ i ] ) ; return ; } } printf ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element STRNEWLINE " ) ; } int main ( ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print2largest ( arr , n ) ; return 0 ; }