Find elements larger than half of the elements in an array | Set 2 | C # program for the above approach ; Function to find the element that are larger than half of elements of the array ; Find the value of mid ; Stores the maximum element ; Stores the frequency of each array element ; Traverse the array in the reverse order ; Decrement the value of count [ i ] and mid ; Print the current element ; Check if the value of mid is equal to 0 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findLarger ( int [ ] arr , int n ) { int mid = ( n + 1 ) / 2 ; int mx = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > mx ) mx = arr [ i ] ; } int [ ] count = new int [ mx + 1 ] ; Array . Clear ( count , 0 , mx + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( int i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; Console . Write ( i + " ▁ " ) ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } public static void Main ( ) { int [ ] arr = { 10 , 4 , 2 , 8 , 9 } ; int N = arr . Length ; findLarger ( arr , N ) ; } }