Count majority element in a matrix
Given a NxM matrix of integers containing duplicate elements. The task is to find the count of all majority occurring elements in the given matrix, where majority element are those whose frequency is greater than or equal to (N*M)/2.
Examples:
Input : mat[] = {{1, 1, 2},
{2, 3, 3},
{4, 3, 3}}
Output : 1
The majority elements is 3 and, its frequency is 4.
Input : mat[] = {{20, 20},
{40, 40}}
Output : 2
Approach:
- Traverse the matrix and use a map in C++ to store the frequency of elements of the matrix such that the key of map is the matrix element and value is its frequency in the matrix.
- Then, traverse the map to find the frequency of elements with a count variable to count majority elements and check if it is equal to or greater than (N*M)/2. If true, then increase the count.
Below is the implementation of the above approach:
C++
// C++ program to find count of all // majority elements in a Matrix #include <bits/stdc++.h> using namespace std; #define N 3 // Rows #define M 3 // Columns // Function to find count of all // majority elements in a Matrix int majorityInMatrix(int arr[N][M]) { unordered_map<int, int> mp; // Store frequency of elements // in matrix for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { mp[arr[i][j]]++; } } // loop to iteratre through map int countMajority = 0; for (auto itr = mp.begin(); itr != mp.end(); itr++) { // check if frequency is greater than // or equal to (N*M)/2 if (itr->second >= ((N * M) / 2)) { countMajority++; } } return countMajority; } // Driver Code int main() { int mat[N][M] = { { 1, 2, 2 }, { 1, 3, 2 }, { 1, 2, 6 } }; cout << majorityInMatrix(mat) << endl; return 0; } |
Java
// Java program to find count of all // majority elements in a Matrix import java.util.*; class GFG { static int N = 3; // Rows static int M = 3; // Columns // Function to find count of all // majority elements in a Matrix static int majorityInMatrix(int arr[][]) { HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); // Store frequency of elements // in matrix for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if(mp.containsKey(arr[i][j])) mp.put(arr[i][j], mp.get(arr[i][j]) + 1 ); else mp.put(arr[i][j], 1); } } // loop to iteratre through map int countMajority = 0; Iterator<HashMap.Entry<Integer, Integer>> itr = mp.entrySet().iterator(); while(itr.hasNext()) { // check if frequency is greater than // or equal to (N*M)/2 HashMap.Entry<Integer, Integer> entry = itr.next(); if (entry.getValue() >= ((N * M) / 2)) { countMajority++; } } return countMajority; } // Driver Code public static void main (String[] args) { int mat[][] = { { 1, 2, 2 }, { 1, 3, 2 }, { 1, 2, 6 } }; System.out.println(majorityInMatrix(mat)); } } // This code is contributed by ihritik |
Python3
# Python 3 program to find count of all # majority elements in a Matrix N = 3 # Rows M = 3 # Columns # Function to find count of all # majority elements in a Matrix def majorityInMatrix(arr): # we take length equal to max # value in array mp = {i:0 for i in range(7)} # Store frequency of elements # in matrix for i in range(len(arr)): for j in range(len(arr)): mp[arr[i][j]] += 1 # loop to iteratre through map countMajority = 0 for key, value in mp.items(): # check if frequency is greater than # or equal to (N*M)/2 if (value >= (int((N * M) / 2))): countMajority += 1 return countMajority # Driver Code if __name__ == '__main__': mat = [[1, 2, 2], [1, 3, 2], [1, 2, 6]] print(majorityInMatrix(mat)) # This code is contributed by # Shashank_Sharma |
C#
// C# program to find count of all // majority elements in a Matrix using System; using System.Collections.Generic; class GFG { static int N = 3; // Rows static int M = 3; // Columns // Function to find count of all // majority elements in a Matrix static int majorityInMatrix(int [ , ]arr) { Dictionary<int, int> mp = new Dictionary<int, int>(); // Store frequency of elements // in matrix 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; } } // loop to iteratre through map int countMajority = 0; Dictionary<int, int>.KeyCollection keyColl = mp.Keys; foreach( int i in keyColl) { // check if frequency is greater than // or equal to (N*M)/2 if ( mp[i] >= ((N * M) / 2)) { countMajority++; } } return countMajority; } // Driver Code public static void Main () { int [, ] mat = { { 1, 2, 2 }, { 1, 3, 2 }, { 1, 2, 6 } }; Console.WriteLine(majorityInMatrix(mat)); } } // This code is contributed by ihritik |
1
Time Complexity: O(N x M)
Auxiliary Space: O(N X M)
Further Optimization :
We can use Moore’s voting algorithm to solve above problem in O(1) extra space. We can simply consider matrix elements as one dimensional array.
Recommended Posts:
- Majority Element | Set-2 (Hashing)
- Majority element in a linked list
- Check if an array has a majority element
- Majority element in a circular array of 0's and 1's
- Count rows in a matrix that consist of same element
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Sum of columns of a 2-D Matrix where first element is odd
- Minimum element of each row and each column in a matrix
- Find maximum element of each row in a matrix
- Search element in a sorted matrix
- Problem of 8 Neighbours of an element in a 2-D Matrix
- Search element in a Spirally sorted Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



