Find two duplicate elements in an limited range array (using XOR)

Given an array of integers with all its elements between 1 to n with the exception of two elements which occur twice. Find two duplicate elements without using any extra memory.

Expected time complexity: O(n)

 

For example,
 

Input: arr = [4, 3, 6, 5, 2, 4, 1, 1]

Output: Duplicate elements are 1 and 4

 

Related Posts: Find two odd occurring element in an array without using any extra space

 


 

We can use hashing to solve this problem in O(n) time. The idea is to traverse the array and maintain frequency of each element in a hash table. Then after all array elements are processed, we return the elements with frequency of two. The problem with this approach is that it requires O(n) extra space as well. Also it requires one traversal of array and one traversal of hash table. The advantage of this approach is its simplicity and the fact that it will work for any number of duplicates elements present in the array.
 

We can solve this problem in one traversal of array and in O(1) space. The idea is to use XOR operator. We know that if we XOR a number with itself odd number of times the result is number itself, otherwise if we XOR a number even number of times with itself, the result is 0. Also XOR with 0 is always the number itself.


XOR x with 0
x ^ 0 = x
 

XOR x with itself even number of times
x ^ x = 0
x ^ x ^ x ^ x = (x ^ x) ^ (x ^ x) = 0 ^ 0 = 0
 

XOR x with itself odd number of times
(x ^ x ^ x) = (x ^ (x ^ x)) = (x ^ 0) = x
(x ^ x ^ x ^ x ^ x) = (x ^ (x ^ x) ^ (x ^ x)) = (x ^ 0 ^ 0) = x

 

So, if we take XOR of all elements present in the array with every element in the range [1 .. n], even appearing elements will cancel out each other and we are left with XOR of x and y (x ^ y) where x and y are two duplicate elements. Note that the duplicate elements will be XORed 3 times in total, whereas all others elements will be XORed twice.
 

How to find x and y?
 

res = (x ^ y)

We know that any set bit in res will be either set in x or y (but not in both as a bit will only set in res when it is set in one number and unset in the other).

The idea is to consider the rightmost set bit in res (or any other set bit) and split the array/range into two lists –

  1. First list contains all elements of the array and numbers in range that have this bit set.
     
  2. Second list contains all elements of the array and numbers in range that have this bit unset.

As this rightmost bit is set in one number and unset in the other, we will have one duplicate element in each list. Basically we have isolated trait of one number with other so that both x and y will go to different lists.

Now we iterate each list once more, do XOR on each element of the list and the result will be the duplicate element present in that list (since elements appearing twice will cancel each other).

C++

Download   Run Code

Output:

Duplicate elements are 1 and 4

Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz