Count majority element in a matrix | Function to find count of all majority elements in a Matrix ; Store frequency of elements in matrix ; loop to iteratre through map ; check if frequency is greater than or equal to ( N * M ) / 2 ; Driver Code
function majorityInMatrix ( arr ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { if ( mp . has ( arr [ i ] [ j ] ) ) { mp . set ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) } else mp . set ( arr [ i ] [ j ] , 1 ) ; } } var countMajority = 0 ; mp . forEach ( ( value , key ) => { if ( value >= ( parseInt ( ( N * M ) / 2 ) ) ) { countMajority ++ ; } } ) ; return countMajority ; } var mat = [ [ 1 , 2 , 2 ] , [ 1 , 3 , 2 ] , [ 1 , 2 , 6 ] ] ; document . write ( majorityInMatrix ( mat ) ) ;