# Sum of all minimum frequency elements in Matrix

Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all minimum occurring elements in the given matrix. That is the sum of all such elements whose frequency is even in the matrix.

**Examples**:

Input: mat[] = {{1, 1, 2}, {2, 3, 3}, {4, 5, 3}}Output: 9 The min occurring elements are 4, 5 and they occurs only 1 time. Therefore, sum = 4+5 = 9Input: mat[] = {{10, 20}, {40, 40}}Output: 30

**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 minimum frequency.
- Finally, traverse the map to find the frequency of elements and check if it matches with the minimum frequency obtained in previous step, if yes, then add this element it’s frequency times to sum.

Below is the implementation of the above approach:

## C++

`// C++ program to find sum of all min ` `// frequency elements in a Matrix ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define N 3 // Rows ` `#define M 3 // Columns ` ` ` `// Function to find sum of all min ` `// frequency elements in a Matrix ` `int` `sumMinOccurring(` `int` `arr[N][M]) ` `{ ` ` ` `// Store frequencies of elements ` ` ` `// in matrix ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `for` `(` `int` `j = 0; j < M; j++) { ` ` ` `mp[arr[i][j]]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Find minimum frequency ` ` ` `int` `sum = 0; ` ` ` `int` `minFreq = INT_MAX; ` ` ` `for` `(` `auto` `itr = mp.begin(); itr != mp.end(); itr++) { ` ` ` `if` `(itr->second < minFreq) ` ` ` `minFreq = itr->second; ` ` ` `} ` ` ` ` ` `// Sum of minimum frequency elements ` ` ` `for` `(` `auto` `itr = mp.begin(); itr != mp.end(); itr++) { ` ` ` `if` `(itr->second == minFreq) { ` ` ` `sum += (itr->first) * (itr->second); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `mat[N][M] = { { 1, 2, 3 }, ` ` ` `{ 1, 3, 2 }, ` ` ` `{ 1, 5, 6 } }; ` ` ` ` ` `cout << sumMinOccurring(mat) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

// Java program to find sum of all min

// frequency elements in a Matrix

import java.util.HashMap;

import java.util.Iterator;

class GFG

{

static int N = 3; // Rows

static int M = 3; // Columns

// Function to find sum of all min

// frequency elements in a Matrix

public static int sumMinOccuring(int[][] arr)

{

// Store frequencies of elements

// in matrix

HashMap

for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (mp.containsKey(arr[i][j]))
{
int x = mp.get(arr[i][j]);
mp.put(arr[i][j], x + 1);
}
else
mp.put(arr[i][j], 1);
}
}
// Find minimum frequency
int sum = 0;
int minFreq = Integer.MAX_VALUE;
for (HashMap.Entry

{

if (entry.getValue() < minFreq)
minFreq = entry.getValue();
}
// Sum of minimum frequency elements
for (HashMap.Entry

{

if (entry.getValue() == minFreq)

sum += entry.getKey() * entry.getValue();

}

return sum;

}

// Driver code

public static void main(String[] args)

{

int[][] mat = { { 1, 2, 3 },

{ 1, 3, 2 },

{ 1, 5, 6 } };

System.out.println(sumMinOccuring(mat));

}

}

// This code is contributed by

// sanjeev2552

## Python3

`# Python3 program to find sum of all min ` `# frequency elements in a Matrix ` ` ` `import` `sys ` `import` `math ` ` ` `# Store frequencies of elements ` `# in matrix ` `def` `sumMinOccuring(mat): ` ` ` `n,m` `=` `len` `(mat),` `len` `(mat[` `0` `]) ` ` ` `_map` `=` `{} ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(m): ` ` ` `d` `=` `mat[i][j] ` ` ` `if` `d ` `in` `_map: ` ` ` `_map[d]` `=` `_map.get(d)` `+` `1` ` ` `else` `: ` ` ` `_map[d]` `=` `1` ` ` ` ` `# Find minimum frequency ` ` ` `_sum,minFreq` `=` `0` `,sys.maxsize ` ` ` `for` `i ` `in` `_map: ` ` ` `minFreq` `=` `min` `(minFreq,_map.get(i)) ` ` ` ` ` `# Sum of minimum frequency elements ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(m): ` ` ` `if` `_map.get(mat[i][j])` `=` `=` `minFreq: ` ` ` `_sum` `+` `=` `mat[i][j] ` ` ` ` ` `return` `_sum ` ` ` `# Driver Code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `mat` `=` `[[` `1` `,` `2` `,` `3` `],[` `1` `,` `3` `,` `2` `],[` `1` `,` `5` `,` `6` `]] ` ` ` `print` `(sumMinOccuring(mat)) ` ` ` ` ` `# This code is Contributed by Vikash Kumar 37 ` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity :** O(M x N)

**Auxiliary Space :** O(M x N)

## Recommended Posts:

- Sum of all even frequency elements in Matrix
- Sum of all odd frequency elements in a Matrix
- Sum of all maximum frequency elements in Matrix
- Count minimum frequency elements in a linked list
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Minimum operations required to set all elements of binary matrix
- Minimum operations of given type to make all elements of a matrix equal
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Sum of all odd frequency elements in an array
- Sort elements by frequency | Set 5 (using Java Map)
- Sum of elements in an array having prime frequency
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimum steps required to convert the matrix into lower hessenberg 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.