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++
// CPP program to find the sum of all odd // occurring elements in an array #include <bits/stdc++.h> using namespace std; // Function to find the sum of all odd // occurring elements in an array int findSum(int arr[], int N) { // Store frequencies of elements // of the array unordered_map<int, int> mp; for (int i = 0; i < N; i++) mp[arr[i]]++; // variable to store sum of all // odd occurring elements int sum = 0; // loop to iterate through map for (auto itr = mp.begin(); itr != mp.end(); itr++) { // check if frequency is odd if (itr->second % 2 != 0) sum += (itr->first) * (itr->second); } return sum; } // Driver Code int main() { int arr[] = { 10, 20, 20, 10, 40, 40, 10 }; int N = sizeof(arr) / sizeof(arr[0]); cout << findSum(arr, N); return 0; } |
chevron_right
filter_none
Java
// Java program to find the sum of all odd // occurring elements in an array import java.util.*; class GFG { // Function to find the sum of all odd // occurring elements in an array static int findSum(int arr[], int N) { // Store frequencies of elements // of the array 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); // variable to store sum of all // odd occurring elements int sum = 0; // loop to iterate through map for (Map.Entry<Integer,Integer> entry : mp.entrySet()) { // check if frequency is odd if (entry.getValue() % 2 != 0) sum += (entry.getKey()) * (entry.getValue()); } return sum; } // Driver Code 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)); } } /* This code is contributed by PrinciRaj1992 */ |
chevron_right
filter_none
Python3
# Function to find sum of all odd # occurring elements in an array import collections def findsum(arr, N): # Store frequencies of elements # of an array in dictionary mp = collections.defaultdict(int) for i in range(N): mp[arr[i]] += 1 # Variable to store sum of all # odd occurring elements sum = 0 # loop to iterate through dictionary for i in mp: # Check if frequency is odd if (mp[i] % 2 != 0): sum += (i * mp[i]) return sum # Driver Code arr = [ 10, 20, 20, 10, 40, 40, 10 ] N = len(arr) print (findsum(arr, N)) # This cde is contributed # by HardeepSingh. |
chevron_right
filter_none
C#
// C# program to find the sum of all odd // occurring elements in an array using System; using System.Collections.Generic; class GFG { // Function to find the sum of all odd // occurring elements in an array public static int findSum(int[] arr, int N) { // Store frequencies of elements // of the array 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); } // variable to store sum of all // odd occurring elements int sum = 0; // loop to iterate through map foreach (KeyValuePair<int, int> entry in mp) { // check if frequency is odd if (entry.Value % 2 != 0) sum += entry.Key * entry.Value; } return sum; } // Driver code public static void Main(String[] args) { int[] arr = { 10, 20, 20, 10, 40, 40, 10 }; int n = arr.Length; Console.WriteLine(findSum(arr, n)); } } // This code is contributed by // sanjeev2552 |
chevron_right
filter_none
Output:
30
Time Complexity: O(N), where N is the number of elements in the array.
Recommended Posts:
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- XOR of elements in an array having prime frequency
- Sum of elements in an array having prime frequency
- Array range queries for elements with frequency same as value
- Find the element having different frequency than other array elements
- Product of elements in an array having prime frequency
- Replace every elements in the array by its frequency in the array
- Remove elements from the array whose frequency lies in the range [l, r]
- Fill an array based on frequency where elements are in range from 0 to n-1
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Sort elements by frequency | Set 2
- Sum of all even frequency elements in Matrix
- Sum of all odd frequency elements in a Matrix
- Sort elements by frequency | Set 1
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.



