Given an array of N integers. Each element in the array occurs same number of times except one element. The task is to find this element.

**Examples:**

Input :arr[] = {1, 1, 2, 2, 3}Output :3Input :arr[] = {0, 1, 2, 4, 4}Output :4

The idea is to use a hash table **freq** to store the frequencies of given elements. Once we have frequencies in the hash table, we can traverse the table to find the only value which is different from others.

Below is the implementation of the above idea :

## C++

`// C++ program to find the element having ` `// different frequency than other array ` `// elements having same frequency ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the element having ` `// different frequency from other array ` `// elements with same frequency ` `int` `findElement(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Store frequencies of elements ` ` ` `unordered_map<` `int` `, ` `int` `> freq; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// increase the value by 1 for every ` ` ` `// time the element occurs in an array ` ` ` `freq[arr[i]]++; ` ` ` `} ` ` ` ` ` `// Below code is used find the only different ` ` ` `// value in freq. ` ` ` `auto` `it = freq.begin(); ` ` ` `int` `fst_fre = it->second, fst_ele = it->first; ` ` ` `if` `(freq.size() <= 2) ` ` ` `return` `fst_fre; ` ` ` `it++; ` ` ` `int` `sec_fre = it->second, sec_ele = it->first; ` ` ` `it++; ` ` ` `int` `trd_fre = it->second, trd_ele = it->first; ` ` ` `if` `(sec_fre == fst_fre && sec_fre != trd_fre) ` ` ` `return` `trd_ele; ` ` ` `if` `(sec_fre == trd_fre && sec_fre != fst_fre) ` ` ` `return` `fst_ele; ` ` ` `if` `(fst_fre == trd_fre && sec_fre != fst_fre) ` ` ` `return` `sec_ele; ` ` ` ` ` `// We reach here when first three frequencies are same ` ` ` `it++; ` ` ` `for` `(; it != freq.end(); it++) { ` ` ` `if` `(it->second != fst_fre) ` ` ` `return` `it->first; ` ` ` `} ` ` ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 0, 1, 2, 4, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << findElement(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the element having ` `# different frequency than other array ` `# elements having same frequency ` ` ` `# Function for above implementation ` `def` `findElement(arr, n) : ` ` ` ` ` `# Empty dictionary to hold the values ` ` ` `freq ` `=` `{} ` ` ` ` ` `# Initialization of frequencies of each ` ` ` `# element to 0 ` ` ` `for` `i ` `in` `range` `(` `0` `, n) : ` ` ` ` ` `freq[arr[i]] ` `=` `0` ` ` ` ` `# Count of frequencies of elements ` ` ` `for` `i ` `in` `range` `(` `0` `, n) : ` ` ` ` ` `freq[arr[i]] ` `=` `freq[arr[i]] ` `+` `1` ` ` ` ` `# Storing the first value of dictionary ` ` ` `trd_ele ` `=` `freq[` `0` `] ` ` ` ` ` `# Variable to hold the final result ` ` ` `position ` `=` `-` `1` ` ` ` ` `# Following loop iterates through the dictionary ` ` ` `# and checks if frequencies are different ` ` ` `# from the frequency of the first element ` ` ` `for` `i ` `in` `freq : ` ` ` ` ` `flag ` `=` `freq[i] ` ` ` ` ` `if` `trd_ele !` `=` `flag : ` ` ` ` ` `# Difference has been detected ` ` ` `position ` `=` `i ` ` ` `break` ` ` ` ` `# Following lines of code checks if the first ` ` ` `# element is itself the required anomaly by ` ` ` `# comparing the frequencies of first 3 elements ` ` ` `fst_ele ` `=` `freq[` `1` `] ` ` ` `sec_ele ` `=` `freq[` `2` `] ` ` ` ` ` `if` `trd_ele !` `=` `fst_ele : ` ` ` ` ` `if` `trd_ele !` `=` `sec_ele : ` ` ` ` ` `for` `i ` `in` `freq : ` ` ` ` ` `# First element is the desired result ` ` ` `position ` `=` `i ` ` ` `break` ` ` ` ` `# Final result is returned ` ` ` `return` `position ` ` ` ` ` `# Driver code ` `arr ` `=` `[ ` `0` `, ` `1` `, ` `2` `, ` `4` `, ` `4` `] ` `# Variable to store length of array ` `n ` `=` `len` `(arr) ` `print` `(findElement(arr, n)) ` ` ` `# This code is contributed by Pratik Basu ` |

*chevron_right*

*filter_none*

**Output:**

4

Time Complexity : O(n)

Auxiliary Space : O(n)

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:

- Find element in array with frequency equal to sum of frequencies of other elements
- Find element with highest frequency in given nested Array
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Find frequency of each element in a limited range array in less than O(n) time
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Replace each element by the difference of the total size of the array and frequency of that element
- Find Kth element in an array containing odd elements first and then even elements
- Find element in array that divides all array elements
- Find an array element such that all elements are divisible by it
- Smallest subarray having an element with frequency greater than that of other elements
- Find the index of the element in an array which divides most elements before it
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Find the element in a linked list with frequency at least N/3
- Maximum element in an array which is equal to its frequency
- Check if frequency of each element in given array is unique or not
- Frequency of each element of an array of small ranged values
- Sorting element of an array by frequency in decreasing order
- Cumulative frequency of count of each element in an unsorted array
- Sum of all odd frequency elements in an array
- XOR of elements in an array having prime frequency

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.