Sum of all odd frequency elements in a Matrix
Last Updated :
24 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define N 3 // Rows
#define M 3 // Columns
int sumOddOccurring( int arr[N][M])
{
map< int , int > mp;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
mp[arr[i][j]]++;
}
}
int sum = 0;
for ( auto itr = mp.begin(); itr != mp.end(); itr++) {
if (itr->second % 2 != 0) {
sum += (itr->first) * (itr->second);
}
}
return sum;
}
int main()
{
int mat[N][M] = { { 1, 2, 3 },
{ 1, 3, 2 },
{ 1, 5, 6 } };
cout << sumOddOccurring(mat) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int N = 3 ;
static int M = 3 ;
static int sumOddOccurring( int arr[][])
{
Map<Integer, Integer> mp = new HashMap<>();
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 );
}
}
}
int sum = 0 ;
for (Map.Entry<Integer, Integer> itr : mp.entrySet())
{
if (itr.getValue() % 2 != 0 )
{
sum += (itr.getKey()) * (itr.getValue());
}
}
return sum;
}
public static void main(String[] args)
{
int mat[][] = {{ 1 , 2 , 3 },
{ 1 , 3 , 2 },
{ 1 , 5 , 6 }};
System.out.println(sumOddOccurring(mat));
}
}
|
Python3
def sumOddOccurring(mat):
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 = 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
if __name__ = = '__main__' :
mat = [[ 1 , 2 , 3 ],[ 1 , 3 , 2 ],[ 1 , 5 , 6 ]]
print (sumOddOccurring(mat))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int N = 3;
static int M = 3;
static int sumOddOccurring( 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]))
{
var v = mp[arr[i,j]];
mp.Remove(arr[i,j]);
mp.Add(arr[i,j], ++v);
}
else
{
mp.Add(arr[i,j], 1);
}
}
}
int sum = 0;
foreach (KeyValuePair< int , int > itr in mp)
{
if (itr.Value % 2 != 0)
{
sum += (itr.Key) * (itr.Value);
}
}
return sum;
}
public static void Main(String[] args)
{
int [,]mat = {{1, 2, 3},
{1, 3, 2},
{1, 5, 6}};
Console.WriteLine(sumOddOccurring(mat));
}
}
|
Javascript
<script>
var N = 3;
var M = 3;
function sumOddOccurring(arr)
{
var mp = new Map();
for ( var i = 0; i < N; i++)
{
for ( var j = 0; j < M; j++)
{
if (mp.has(arr[i][j]))
{
var v = mp.get(arr[i][j]);
mp. delete (arr[i][j]);
mp.set(arr[i][j], ++v);
}
else
{
mp.set(arr[i][j], 1);
}
}
}
var sum = 0;
mp.forEach((value, key) => {
if (value % 2 != 0)
{
sum += (key) * (value);
}
});
return sum;
}
var mat = [[1, 2, 3],
[1, 3, 2],
[1, 5, 6]];
document.write(sumOddOccurring(mat));
</script>
|
Complexity Analysis:
- Time Complexity: O(N x M)
- Auxiliary complexity: O(N x M)
Share your thoughts in the comments
Please Login to comment...