# Sum of all odd frequency elements in a Matrix

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

**Examples**:

Input: mat[] = {{1, 1, 2}, {2, 3, 3}, {4, 5, 3}}Output: 18 The odd occurring elements are 3, 4, 5 and their number of occurrences are 3, 1, 1 respectively. Therefore, sum = 3+3+3+4+5 = 18.Input: 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 frequency of elements and check if it is odd, if it is odd, 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 odd ` `// 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 odd ` `// frequency elements in a Matrix ` `int` `sumOddOccurring(` `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]]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sum of odd frequency elements ` ` ` `int` `sum = 0; ` ` ` `for` `(` `auto` `itr = mp.begin(); itr != mp.end(); itr++) { ` ` ` `if` `(itr->second % 2 != 0) { ` ` ` `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 << sumOddOccurring(mat) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

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

*chevron_right*

*filter_none*

**Output:**

14

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

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

## Recommended Posts:

- Sum of all even frequency elements in Matrix
- Sum of all maximum frequency elements in Matrix
- Sum of all minimum frequency elements in Matrix
- 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
- Sum of all odd frequency elements in an array
- Sum of elements in an array having prime frequency
- Sort elements by frequency | Set 5 (using Java Map)
- Find the element having different frequency than other array elements
- Remove elements from the array whose frequency lies in the range [l, r]
- Sort elements by frequency | Set 4 (Efficient approach using hash)
- Count minimum frequency elements in a linked list
- Program to print elements of a Matrix row-wise skipping alternate elements
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?

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.