Sum of all odd frequency elements in an array
Given an array of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given array. That is the sum of all such elements whose frequency is odd in the array.
Examples:
Input : arr[] = {1, 1, 2, 2, 3, 3, 3}
Output : 9
The odd occurring element is 3, and it's number
of occurrence is 3. Therefore sum of all 3's in the
array = 9.
Input : arr[] = {10, 20, 30, 40, 40}
Output : 60
Elements with odd frequency are 10, 20 and 30.
Sum = 60.
Approach:
- Traverse the array and use a map in C++ to store the frequency of elements of the array such that the key of map is the array element and value is its frequency in the array.
- Then, traverse the map to find the frequency of elements and check if it is odd, if it is odd, then add this element to sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int arr[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++)
mp[arr[i]]++;
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 arr[] = { 10, 20, 20, 10, 40, 40, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findSum( int arr[], int N)
{
Map<Integer,Integer> mp = new HashMap<>();
for ( int i = 0 ; i < N; i++)
mp.put(arr[i],mp.get(arr[i])== null ? 1 :mp.get(arr[i])+ 1 );
int sum = 0 ;
for (Map.Entry<Integer,Integer> entry : mp.entrySet())
{
if (entry.getValue() % 2 != 0 )
sum += (entry.getKey()) * (entry.getValue());
}
return sum;
}
public static void main(String args[])
{
int arr[] = { 10 , 20 , 20 , 10 , 40 , 40 , 10 };
int N = arr.length;
System.out.println(findSum(arr, N));
}
}
|
Python3
import collections
def findsum(arr, N):
mp = collections.defaultdict( int )
for i in range (N):
mp[arr[i]] + = 1
sum = 0
for i in mp:
if (mp[i] % 2 ! = 0 ):
sum + = (i * mp[i])
return sum
arr = [ 10 , 20 , 20 , 10 , 40 , 40 , 10 ]
N = len (arr)
print (findsum(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int findSum( int [] arr, int N)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
mp[arr[i]]++;
else
mp.Add(arr[i], 1);
}
int sum = 0;
foreach (KeyValuePair< int ,
int > entry in mp)
{
if (entry.Value % 2 != 0)
sum += entry.Key * entry.Value;
}
return sum;
}
public static void Main(String[] args)
{
int [] arr = { 10, 20, 20, 10, 40, 40, 10 };
int n = arr.Length;
Console.WriteLine(findSum(arr, n));
}
}
|
Javascript
<script>
function findSum(arr, N)
{
let mp = new Map();
for (let i = 0; i < N; i++) {
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1)
} else {
mp.set(arr[i], 1)
}
}
let sum = 0;
for (let itr of mp) {
if (itr[1] % 2 != 0)
sum += (itr[0]) * (itr[1]);
}
return sum;
}
let arr = [10, 20, 20, 10, 40, 40, 10];
let N = arr.length
document.write(findSum(arr, N));
</script>
|
complexity Analysis:
- Time Complexity: O(N), where N is the number of elements in the array.
- Auxiliary Space: O(N)
Method 2:Using Built in python functions:
- Count the frequencies of every element using Counter function
- Traverse the frequency dictionary and sum all the elements with occurrence odd frequency multiplied by its frequency.
Below is the implementation:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
void sumOdd( int arr[], int n)
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
int sum = 0;
for ( auto it : freq) {
if (it.second % 2 != 0) {
sum += it.first * it.second;
}
}
cout << sum << endl;
}
int main() {
int arr[] = {10, 20, 30, 40, 40};
int n = sizeof (arr) / sizeof (arr[0]);
sumOdd(arr, n);
}
|
Python3
from collections import Counter
def sumOdd(arr, n):
freq = Counter(arr)
sum = 0
for it in freq:
if freq[it] % 2 ! = 0 :
sum = sum + it * freq[it]
print ( sum )
arr = [ 10 , 20 , 30 , 40 , 40 ]
n = len (arr)
sumOdd(arr, n)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static void sumOdd( int [] arr, int n)
{
Dictionary< int , int > freq
= arr.GroupBy(x => x).ToDictionary(
x => x.Key, x => x.Count());
int sum = 0;
foreach (KeyValuePair< int , int > entry in freq)
{
if (entry.Value % 2 != 0) {
sum += entry.Key * entry.Value;
}
}
Console.WriteLine(sum);
}
public static void Main()
{
int [] arr = { 10, 20, 30, 40, 40 };
int n = arr.Length;
sumOdd(arr, n);
}
}
|
Javascript
function sumOdd(arr, n) {
let freq = new Map();
for (let i=0; i<n; i++){
if (freq.has(arr[i])){
freq.set(arr[i], freq.get(arr[i])+1);
} else {
freq.set(arr[i], 1);
}
}
let sum = 0;
for (let [key, value] of freq){
if (value % 2 !== 0){
sum += key*value;
}
}
console.log(sum);
}
let arr = [10, 20, 30, 40, 40];
let n = arr.length;
sumOdd(arr, n);
|
Java
import java.util.HashMap;
public class GFG {
static void sumOdd( int [] arr, int n) {
HashMap<Integer, Integer> freq = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
if (freq.containsKey(arr[i])) {
freq.put(arr[i], freq.get(arr[i]) + 1 );
} else {
freq.put(arr[i], 1 );
}
}
int sum = 0 ;
for (HashMap.Entry<Integer, Integer> entry : freq.entrySet()) {
if (entry.getValue() % 2 != 0 ) {
sum += entry.getKey() * entry.getValue();
}
}
System.out.println(sum);
}
public static void main(String[] args) {
int [] arr = { 10 , 20 , 30 , 40 , 40 };
int n = arr.length;
sumOdd(arr, n);
}
}
|
Complexity Analysis:
- Time Complexity: O(N), where N is the number of elements in the array.
- Auxiliary Space: O(N)
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...