# Find the two non-repeating elements in an array of repeating elements

Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n numbers are occurring twice and remaining two have occurred once). Find those two numbers in the most efficient way.

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Method 1(Use Sorting)
First, sort all the elements. In the sorted array, by comparing adjacent elements we can easily get the non-repeating elements. Time complexity of this method is O(nLogn)

Method 2(Use XOR)
Let x and y be the non-repeating elements we are looking for and arr[] be the input array. First, calculate the XOR of all the array elements.

```     xor = arr^arr^arr.....arr[n-1]
```

All the bits that are set in xor will be set in one non-repeating element (x or y) and not in others. So if we take any set bit of xor and divide the elements of the array in two sets – one set of elements with same bit set and another set with same bit not set. By doing so, we will get x in one set and y in another set. Now if we do XOR of all the elements in the first set, we will get the first non-repeating element, and by doing same in other sets we will get the second non-repeating element.

```Let us see an example.
arr[] = {2, 4, 7, 9, 2, 4}
1) Get the XOR of all the elements.
xor = 2^4^7^9^2^4 = 14 (1110)
2) Get a number which has only one set bit of the xor.
Since we can easily get the rightmost set bit, let us use it.
set_bit_no = xor & ~(xor-1) = (1110) & ~(1101) = 0010
Now set_bit_no will have only set as rightmost set bit of xor.
3) Now divide the elements in two sets and do xor of
elements in each set and we get the non-repeating
elements 7 and 9. Please see the implementation for this step.
```

Implementation:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `/* This finction sets the values of  ` `*x and *y to nonr-epeating elements ` `in an array arr[] of size n*/` `void` `get2NonRepeatingNos(``int` `arr[], ``int` `n, ``int` `*x, ``int` `*y)  ` `{  ` `    ``int` `Xor = arr; ``/* Will hold Xor of all elements */` `    ``int` `set_bit_no; ``/* Will have only single set bit of Xor */` `    ``int` `i;  ` `    ``*x = 0;  ` `    ``*y = 0;  ` `     `  `    ``/* Get the Xor of all elements */` `    ``for``(i = 1; i < n; i++)  ` `    ``Xor ^= arr[i];  ` `     `  `    ``/* Get the rightmost set bit in set_bit_no */` `    ``set_bit_no = Xor & ~(Xor-1);  ` `     `  `    ``/* Now divide elements in two sets by comparing rightmost set  ` `    ``bit of Xor with bit at same position in each element. */` `    ``for``(i = 0; i < n; i++)  ` `    ``{  ` `        ``if``(arr[i] & set_bit_no)  ` `        ``*x = *x ^ arr[i]; ``/*Xor of first set */` `        ``else` `        ``*y = *y ^ arr[i]; ``/*Xor of second set*/` `    ``}  ` `}  ` ` `  `/* Driver code */` `int` `main()  ` `{  ` `    ``int` `arr[] = {2, 3, 7, 9, 11, 2, 3, 11};  ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(*arr); ` `    ``int` `*x = ``new` `int``[(``sizeof``(``int``))];  ` `    ``int` `*y = ``new` `int``[(``sizeof``(``int``))];  ` `    ``get2NonRepeatingNos(arr, n, x, y);  ` `    ``cout<<``"The non-repeating elements are "``<<*x<<``" and "``<<*y;  ` `}  ` ` `  `// This code is contributed by rathbhupendra `

## C

 `#include ` `#include ` ` `  `/* This finction sets the values of *x and *y to nonr-epeating ` ` ``elements in an array arr[] of size n*/` `void` `get2NonRepeatingNos(``int` `arr[], ``int` `n, ``int` `*x, ``int` `*y) ` `{ ` `  ``int` `xor = arr; ``/* Will hold xor of all elements */` `  ``int` `set_bit_no;  ``/* Will have only single set bit of xor */` `  ``int` `i; ` `  ``*x = 0; ` `  ``*y = 0; ` ` `  `  ``/* Get the xor of all elements */` `  ``for``(i = 1; i < n; i++) ` `   ``xor ^= arr[i]; ` ` `  `  ``/* Get the rightmost set bit in set_bit_no */` `  ``set_bit_no = xor & ~(xor-1); ` ` `  `  ``/* Now divide elements in two sets by comparing rightmost set ` `   ``bit of xor with bit at same position in each element. */` `  ``for``(i = 0; i < n; i++) ` `  ``{ ` `    ``if``(arr[i] & set_bit_no) ` `     ``*x = *x ^ arr[i]; ``/*XOR of first set */` `    ``else` `     ``*y = *y ^ arr[i]; ``/*XOR of second set*/` `  ``} ` `} ` ` `  `/* Driver program to test above function */` `int` `main() ` `{ ` `  ``int` `arr[] = {2, 3, 7, 9, 11, 2, 3, 11}; ` `  ``int` `*x = (``int` `*)``malloc``(``sizeof``(``int``)); ` `  ``int` `*y = (``int` `*)``malloc``(``sizeof``(``int``)); ` `  ``get2NonRepeatingNos(arr, 8, x, y); ` `  ``printf``(``"The non-repeating elements are %d and %d"``, *x, *y); ` `  ``getchar``(); ` `} `

Output:

`The non-repeating elements are 7 and 9`

Time Complexity: O(n)
Auxiliary Space: O(1)

Please refer below post for detailed explanation :
Find the two numbers with odd occurrences in an unsorted array

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Article Tags :

48

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