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
using System ; using System . Collections . Generic ; class GFG { static int majorityInMatrix ( int [ , ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . ContainsKey ( arr [ i , j ] ) ) mp [ arr [ i , j ] ] ++ ; else mp [ arr [ i , j ] ] = 1 ; } } int countMajority = 0 ; Dictionary < int , int > . KeyCollection keyColl = mp . Keys ; foreach ( int i in keyColl ) { if ( mp [ i ] >= ( ( N * M ) / 2 ) ) { countMajority ++ ; } } return countMajority ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 2 } , { 1 , 3 , 2 } , { 1 , 2 , 6 } } ; Console . WriteLine ( majorityInMatrix ( mat ) ) ; } }