Given an limited range array of integers of size n with all its elements between 1 to n with the exception of one element which occur twice and one number which is missing from the list. Find 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: **Duplicate and missing elements are 4 and 1 respectively

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

We can solve this problem in O(n) time and in O(1) space using **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 is duplicate and missing element. Note that the duplicate element will be XORed 3 times in total and missing element will be XORed 1 times, 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 –

- First list contains all elements of the array and numbers in range that have this bit set.

- Second list contains all elements of the array and numbers in range that have this bit unset.

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

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

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <iostream> #include <algorithm> #include <math.h> using namespace std; // Function to find the missing number and duplicate element // using XOR operator in an array of size n and range of // elements between [1 to n] pair<int, int> findMissingAndDuplicate(int arr[], int n) { // take XOR of all array elements from index [0 to n-1] // and all numbers in range [1 to n] int res = n; for (int i = 0; i < n; i++) res = res ^ arr[i] ^ i; // x, y stores the duplicate element and missing number int x = 0, y = 0; // res stores (x ^ y) // find position of the rightmost set bit in res int k = log2(res & -res); // split the array into two sub-arrays for (int i = 0; i < n; i++) { // array elements that have k'th bit 1 if (arr[i] & (1 << k)) x = x ^ arr[i]; // array elements that have k'th bit 0 else y = y ^ arr[i]; } // split the range [1-n] into two sub-range for (int i = 1; i <= n; i++) { // number i have k'th bit 1 if (i & (1 << k)) x = x ^ i; // number i have k'th bit 0 else y = y ^ i; } return make_pair(x, y); } // Find missing number and duplicate elements in an limited range array int main() { int arr[] = { 4, 3, 6, 5, 2, 4 }; int n = 6; // size of the array is n pair<int, int> p = findMissingAndDuplicate(arr, n); // linear search for missing element if (find(arr, arr + n, p.first) == arr + n) swap(p.first, p.second); cout << "Duplicate and missing elements are " << p.first << " and " << p.second; return 0; } |

`Output:`

Duplicate and missing elements are 4 and 1

**Thanks for reading.**

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply