text: Majority Element | Function to find Majority element in an array ; sentinels ; Update maxCount if count of current element is greater ; If maxCount is greater than n / 2 return the corresponding element ; Driver code ; Function calling
code: function findMajority ( arr , n ) { let maxCount = 0 ; let index = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count > maxCount ) { maxCount = count ; index = i ; } } if ( maxCount > n / 2 ) document . write ( arr [ index ] ) ; else document . write ( " " ) ; } let arr = [ 1 , 1 , 2 , 1 , 3 , 5 , 1 ] ; let n = arr . length ; findMajority ( arr , n ) ;