Given an integer array of size n, with all its elements between 1 and n and one element occurring twice and one element missing. Find the missing number and the duplicate element in linear time and without using any extra memory.

For example,

Input:  arr[] = [4, 3, 6, 5, 2, 4]
 
Output: The duplicate and missing elements are 4 and 1, respectively

Practice this problem

 
Related Posts:

Find two odd occurring elements in an array without using any extra space

 
We can solve this problem in linear time and constant space using the XOR operator. We know that if we XOR a number with itself an odd number of times, the result is a number itself; otherwise, if we XOR a number an even number of times with itself, the result is 0. Also, XOR with 0 is always the number itself.

XOR of ‘x’ with 0:
 
x ^ 0 = x
 
 
XOR of ‘x’ with itself even number of times:
 
x ^ x = 0
x ^ x ^ x ^ x = (x ^ x) ^ (x ^ x) = 0 ^ 0 = 0
 
 
XOR of ‘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

If we take XOR of all array elements with every element in range [1, n], even appearing elements will cancel each other. We are left with XOR of x and y, x ^ y, where x and y is the duplicate and missing element. Note that the duplicate element will be XORed three times in total, and the missing element will be XORed 1 time, whereas all other elements will be XORed twice.

How to find x and y?

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

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

  1. The first list contains all the array elements and numbers in the range that have this bit set.
  2. The second list contains all the array elements and numbers in the range that have this bit unset.

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

Now iterate both lists once more and do XOR on each element. The result will be the duplicate element present in one list and the missing number present in the other list (since elements appearing twice will cancel each other).

 
The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The duplicate and missing elements are 4 and 1

Java


Download  Run Code

Output:

The duplicate and missing elements are 4 and 1

Python


Download  Run Code

Output:

The duplicate and missing elements are (4, 1)