Given an array of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given array. That is the sum of all such elements whose frequency is even in the array.

**Examples**:

Input: arr[] = {1, 1, 2, 2, 3, 3, 3}Output: 6 The even occurring element are 1 and 2 (both occur two times). Therefore sum of all 1's in the array = 1+1+2+2 = 6.Input: arr[] = {10, 20, 30, 40, 40}Output: 80 Element with even frequency are 40. Sum = 40.

**Approach**:

- Traverse the array and use a unordered_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 even, if it is even, then add this element to sum.

Below is the implementation of the above approach:

## C++

`// CPP program to find the sum of all even ` `// occurring elements in an array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the sum of all even ` `// occurring elements in an array ` `int` `findSum(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Map to store frequency of elements ` ` ` `// of the array ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `mp[arr[i]]++; ` ` ` `} ` ` ` ` ` `// variable to stroe sum of all ` ` ` `// even occurring elements ` ` ` `int` `sum = 0; ` ` ` ` ` `// loop to iteratre through map ` ` ` `for` `(` `auto` `itr = mp.begin(); itr != mp.end(); itr++) { ` ` ` ` ` `// check if frequency is even ` ` ` `if` `(itr->second % 2 == 0) ` ` ` `sum += (itr->first) * (itr->second); ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 10, 20, 20, 40, 40 }; ` ` ` ` ` `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 even ` `// occurring elements in an array ` ` ` `import` `java.util.HashMap; ` `import` `java.util.Iterator; ` `import` `java.util.Map; ` `import` `java.util.Map.Entry; ` ` ` `public` `class` `GFG { ` ` ` `public` `static` `int` `element(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `Map<Integer, Integer> map = ` `new` `HashMap<Integer, Integer>(); ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `int` `count = ` `0` `; ` ` ` `if` `(map.get(arr[i]) != ` `null` `) { ` ` ` `count = map.get(arr[i]); ` ` ` `} ` ` ` `map.put(arr[i], count + ` `1` `); ` ` ` `} ` ` ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(Entry<Integer, Integer> entry : map.entrySet()) { ` ` ` `if` `(entry.getValue() % ` `2` `== ` `0` `) { ` ` ` `sum += entry.getKey() * entry.getValue(); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `3` `, ` `3` `, ` `3` `}; ` ` ` ` ` `// sum should be = 1+1+2+2=6; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(element(arr, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

120

**Time Complexity**: O(N), where N is the number of elements in the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Two odd occurring elements in an array where all other occur even times
- Find even occurring elements in an array of limited range
- Find the sum of all highest occurring elements in an Array
- Sum of all minimum occurring elements in an Array
- First element occurring k times in an array
- Find Kth most occurring element in an Array
- Rearrange array such that all even-indexed elements in the Array is even
- Find all array elements occurring more than ⌊N/3⌋ times
- Smallest occurring element in each subsequence
- Count the number of vowels occurring in all the substrings of given string
- Print the most occurring character in an array of strings
- GCD of elements occurring Fibonacci number of times in an Array
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Find an element in array such that sum of left array is equal to sum of right array
- Sum of even numbers at even position
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Count of ways to make Array sum even by removing only one element
- Form an array of distinct elements with each element as sum of an element from each array
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Largest element smaller than current element on left for every element in Array

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.