text: Majority Element | Set | function to print the majority Number ; Driver code
code: function majorityNumber ( arr , n ) { let ans = - 1 ; let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) } else { freq . set ( arr [ i ] , 1 ) } if ( freq . get ( arr [ i ] ) > n / 2 ) ans = arr [ i ] ; } return ans ; } let a = [ 2 , 2 , 1 , 1 , 1 , 2 , 2 ] ; let n = a . length ; document . write ( majorityNumber ( a , n ) ) ;