Count majority element in a matrix | C ++ program to find count of all majority elements 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int majorityInMatrix ( int arr [ N ] [ M ] ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { mp [ arr [ i ] [ j ] ] ++ ; } } int countMajority = 0 ; for ( auto itr = mp . begin ( ) ; itr != mp . end ( ) ; itr ++ ) { if ( itr -> second >= ( ( N * M ) / 2 ) ) { countMajority ++ ; } } return countMajority ; } int main ( ) { int mat [ N ] [ M ] = { { 1 , 2 , 2 } , { 1 , 3 , 2 } , { 1 , 2 , 6 } } ; cout << majorityInMatrix ( mat ) << endl ; return 0 ; }