Find elements larger than half of the elements in an array | Set 2 | 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
function findLarger ( arr , n ) { let mid = ( n + 1 ) / 2 ; let mx = Math . max . apply ( null , arr ) var count = new Array ( mx + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( let i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; document . write ( i + ' ' ) ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } var arr = [ 10 , 4 , 2 , 8 , 9 ] ; var N = 5 ; findLarger ( arr , N ) ;