# Unique element in an array where all elements occur K times except one | Set 2

Given an array **arr[]** where every element occurs **K** times except one element which occurs only once, the task is to find that unique element.

**Examples:**

Input:arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7}, k = 3

Output:7

Explanation:

7 is the only element which occurs once while others occurs k times.

Input:arr[] = {12, 12, 2, 2, 3}, k = 2

Output:3

Explanation:

3 is the only element which occurs once while others occurs k times.

**Approach:** Suppose we have every element **K** times then the difference between the **sum of all elements in the given array** and the **K*sum of all unique elements** is **(K-1) times** the unique element.

**For Example:**

arr[] = {a, a, a, b, b, b, c, c, c, d}, k = 3

unique elements = {a, b, c, d}

Difference = 3*(a + b + c + d) – (a + a + a + b + b + b + c + c + c + d) = 2*dTherefore, Generalizing the equation:

The unique element can be given by:

Below are the steps:

- Store all the element of the given array in the set to get the unique elements.
- Find the sum of all element in the array (say
**sum_array**) and sum of all the element in the set(say**sum_set**). - The unique element is given by
**(K*sum_set – sum_array)/(K – 1)**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that find the unique element ` `// in the array arr[] ` `int` `findUniqueElements(` `int` `arr[], ` `int` `N, ` ` ` `int` `K) ` `{ ` ` ` `// Store all unique element in set ` ` ` `unordered_set<` `int` `> s(arr, arr + N); ` ` ` ` ` `// Sum of all element of the array ` ` ` `int` `arr_sum = accumulate(arr, arr + N, 0); ` ` ` ` ` `// Sum of element in the set ` ` ` `int` `set_sum = accumulate(s.begin(), ` ` ` `s.end(), ` ` ` `0); ` ` ` ` ` `// Print the unique element using formula ` ` ` `cout << (K * set_sum - arr_sum) / (K - 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 12, 1, 12, 3, 12, 1, ` ` ` `1, 2, 3, 2, 2, 3, 7 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `K = 3; ` ` ` ` ` `// Function call ` ` ` `findUniqueElements(arr, N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program for the above approach ` ` ` `# Function that find the unique element ` `# in the array arr[] ` `def` `findUniqueElements(arr, N, K): ` ` ` ` ` `# Store all unique element in set ` ` ` `s ` `=` `set` `() ` ` ` `for` `x ` `in` `arr: ` ` ` `s.add(x) ` ` ` ` ` `# Sum of all element of the array ` ` ` `arr_sum ` `=` `sum` `(arr) ` ` ` ` ` `# Sum of element in the set ` ` ` `set_sum ` `=` `0` ` ` `for` `x ` `in` `s: ` ` ` `set_sum ` `+` `=` `x ` ` ` ` ` `# Print the unique element using formula ` ` ` `print` `((K ` `*` `set_sum ` `-` `arr_sum) ` `/` `/` `(K ` `-` `1` `)) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[ ` `12` `, ` `1` `, ` `12` `, ` `3` `, ` `12` `, ` `1` `, ` ` ` `1` `, ` `2` `, ` `3` `, ` `2` `, ` `2` `, ` `3` `, ` `7` `] ` ` ` `N ` `=` `len` `(arr) ` ` ` `K ` `=` `3` ` ` ` ` `# Function call ` ` ` `findUniqueElements(arr, N, K) ` ` ` `# This code is contributed by Samarth ` |

*chevron_right*

*filter_none*

**Output:**

7

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

*, where K is the frequency.*

**Auxiliary Space Complexity:**O(N/K)Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Unique element in an array where all elements occur k times except one
- Two odd occurring elements in an array where all other occur even times
- GCD of elements which occur prime number of times
- Sum of array elements after every element x is XORed itself x times
- Print all the sum pairs which occur maximum number of times
- Minimum deletions required such that any number X will occur exactly X times
- First element occurring k times in an array
- Reduce the array such that each element appears at most 2 times
- Reduce the array such that each element appears at most K times
- Smallest element in an array that is repeated exactly 'k' times.
- Check whether K times of a element is present in array
- First element that appears even number of times in an array
- Largest element in the array that is repeated exactly k times
- Find the K-th minimum element from an array concatenated M times
- Check if the last element of array is even or odd after performing a operation p times
- Find sum of all unique elements in the array for K queries
- Subsequence pair from given Array having all unique and all same elements respectively
- Remove elements from the array which appear more than k times
- Sum of all elements repeating 'k' times in an array
- Divide the array into minimum number of sub-arrays having unique elements

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.