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*d

Therefore, Generalizing the equation:
The unique element can be given by: \frac{(K*(\text{sum of unique element}) - (\text{sum of all element}))}{(K - 1)}

Below are the steps:

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

7

Time Complexity: O(N), where N is the number of elements in the array
Auxiliary Space Complexity: O(N/K), where K is the frequency.

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : ipg2016107

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.