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: < ? php function findMajority ( $ arr , $ n ) { $ maxCount = 0 ; $ index = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count ++ ; } if ( $ count > $ maxCount ) { $ maxCount = $ count ; $ index = $ i ; } } if ( $ maxCount > $ n / 2 ) echo $ arr [ $ index ] . " STRNEWLINE " ; else echo " No ▁ Majority ▁ Element " . " STRNEWLINE " ; } $ arr = array ( 1 , 1 , 2 , 1 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; findMajority ( $ arr , $ n ) ;