# Find the two numbers with odd occurrences in an unsorted array

• Difficulty Level : Medium
• Last Updated : 02 Dec, 2021

Given an unsorted array that contains even number of occurrences for all numbers except two numbers. Find the two numbers which have odd occurrences in O(n) time complexity and O(1) extra space.

Examples:

Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our Amazon Test Series. Includes topic-wise practice questions on all important DSA topics along with 10 practice contests of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!

```Input: {12, 23, 34, 12, 12, 23, 12, 45}
Output: 34 and 45

Input: {4, 4, 100, 5000, 4, 4, 4, 4, 100, 100}
Output: 100 and 5000

Input: {10, 20}
Output: 10 and 20```

A naive method to solve this problem is to run two nested loops. The outer loop picks an element and the inner loop counts the number of occurrences of the picked element. If the count of occurrences is odd then print the number. The time complexity of this method is O(n^2).

We can use sorting to get the odd occurring numbers in O(nLogn) time. First sort the numbers using an O(nLogn) sorting algorithm like Merge Sort, Heap Sort.. etc. Once the array is sorted, all we need to do is a linear scan of the array and print the odd occurring number.

We can also use hashing. Create an empty hash table which will have elements and their counts. Pick all elements of input array one by one. Look for the picked element in hash table. If the element is found in hash table, increment its count in table. If the element is not found, then enter it in hash table with count as 1. After all elements are entered in hash table, scan the hash table and print elements with odd count. This approach may take O(n) time on average, but it requires O(n) extra space.

A O(n) time and O(1) extra space solution:
The idea is similar to method 2 of this post. Let the two odd occurring numbers be x and y. We use bitwise XOR to get x and y. The first step is to do XOR of all elements present in array. XOR of all elements gives us XOR of x and y because of the following properties of XOR operation.
1) XOR of any number n with itself gives us 0, i.e., n ^ n = 0
2) XOR of any number n with 0 gives us n, i.e., n ^ 0 = n
3) XOR is cumulative and associative.

So we have XOR of x and y after the first step. Let the value of XOR be xor2. Every set bit in xor2 indicates that the corresponding bits in x and y have values different from each other. For example, if x = 6 (0110) and y is 15 (1111), then xor2 will be (1001), the two set bits in xor2 indicate that the corresponding bits in x and y are different. In the second step, we pick a set bit of xor2 and divide array elements in two groups. Both x and y will go to different groups. In the following code, the rightmost set bit of xor2 is picked as it is easy to get rightmost set bit of a number. If we do XOR of all those elements of array which have the corresponding bit set (or 1), then we get the first odd number. And if we do XOR of all those elements which have the corresponding bit 0, then we get the other odd occurring number. This step works because of the same properties of XOR. All the occurrences of a number will go in same set. XOR of all occurrences of a number which occur even number of times will result in 0 in its set. And the xor of a set will be one of the odd occurring elements.

## C++

 `// C++ Program to find the two odd occurring elements``#include ``using` `namespace` `std;` `/* Prints two numbers that occur odd number of times. The``function assumes that the array size is at least 2 and``there are exactly two numbers occurring odd number of times. */``void` `printTwoOdd(``int` `arr[], ``int` `size)``{``    ``int` `xor2 = arr; ``/* Will hold XOR of two odd occurring elements */``    ``int` `set_bit_no; ``/* Will have only single set bit of xor2 */``    ``int` `i;``    ``int` `n = size - 2;``    ``int` `x = 0, y = 0;``    ` `    ``/* Get the xor of all elements in arr[]. The xor will basically``        ``be xor of two odd occurring elements */``    ``for``(i = 1; i < size; i++)``        ``xor2 = xor2 ^ arr[i];``    ` `    ``/* Get one set bit in the xor2. We get rightmost set bit``        ``in the following line as it is easy to get */``    ``set_bit_no = xor2 & ~(xor2-1);``    ` `    ``/* Now divide elements in two sets:``        ``1) The elements having the corresponding bit as 1.``        ``2) The elements having the corresponding bit as 0. */``    ``for``(i = 0; i < size; i++)``    ``{``        ``/* XOR of first set is finally going to hold one odd``        ``occurring number x */``        ``if``(arr[i] & set_bit_no)``        ``x = x ^ arr[i];``    ` `        ``/* XOR of second set is finally going to hold the other``        ``odd occurring number y */``        ``else``        ``y = y ^ arr[i];``    ``}` `    ``cout << ``"The two ODD elements are "` `<< x << ``" & "` `<< y;``}` `/* Driver code */``int` `main()``{``    ``int` `arr[] = {4, 2, 4, 5, 2, 3, 3, 1};``    ``int` `arr_size = ``sizeof``(arr)/``sizeof``(arr);``    ``printTwoOdd(arr, arr_size);``    ``return` `0;``}` `// This is code is contributed by rathbhupendra`

## C

 `// Program to find the two odd occurring elements``#include` `/* Prints two numbers that occur odd number of times. The``   ``function assumes that the array size is at least 2 and``   ``there are exactly two numbers occurring odd number of times. */``void` `printTwoOdd(``int` `arr[], ``int` `size)``{``  ``int` `xor2 = arr; ``/* Will hold XOR of two odd occurring elements */``  ``int` `set_bit_no;  ``/* Will have only single set bit of xor2 */``  ``int` `i;``  ``int` `n = size - 2;``  ``int` `x = 0, y = 0;` `  ``/* Get the xor of all elements in arr[]. The xor will basically``     ``be xor of two odd occurring elements */``  ``for``(i = 1; i < size; i++)``    ``xor2 = xor2 ^ arr[i];` `  ``/* Get one set bit in the xor2. We get rightmost set bit``     ``in the following line as it is easy to get */``  ``set_bit_no = xor2 & ~(xor2-1);` `  ``/* Now divide elements in two sets:``    ``1) The elements having the corresponding bit as 1.``    ``2) The elements having the corresponding bit as 0.  */``  ``for``(i = 0; i < size; i++)``  ``{``     ``/* XOR of first set is finally going to hold one odd``       ``occurring number x */``    ``if``(arr[i] & set_bit_no)``      ``x = x ^ arr[i];` `     ``/* XOR of second set is finally going to hold the other``       ``odd occurring number y */``    ``else``      ``y = y ^ arr[i];``  ``}` `  ``printf``(``"\n The two ODD elements are %d & %d "``, x, y);``}` `/* Driver program to test above function */``int` `main()``{``  ``int` `arr[] = {4, 2, 4, 5, 2, 3, 3, 1};``  ``int` `arr_size = ``sizeof``(arr)/``sizeof``(arr);``  ``printTwoOdd(arr, arr_size);``  ``getchar``();``  ``return` `0;``}`

## Java

 `// Java program to find two odd``// occurring elements` `import` `java.util.*;` `class` `Main``{  ``     ` `    ``/* Prints two numbers that occur odd``       ``number of times. The function assumes``       ``that the array size is at least 2 and``       ``there are exactly two numbers occurring``       ``odd number of times. */``    ``static` `void` `printTwoOdd(``int` `arr[], ``int` `size)``    ``{``      ``/* Will hold XOR of two odd occurring elements */`   `      ``int` `xor2 = arr[``0``];``      ` `      ``/* Will have only single set bit of xor2 */``      ``int` `set_bit_no; ``      ``int` `i;``      ``int` `n = size - ``2``;``      ``int` `x = ``0``, y = ``0``;``     ` `      ``/* Get the xor of all elements in arr[].``         ``The xor will basically be xor of two``         ``odd occurring elements */``      ``for``(i = ``1``; i < size; i++)``        ``xor2 = xor2 ^ arr[i];``     ` `      ``/* Get one set bit in the xor2. We get``         ``rightmost set bit in the following``         ``line as it is easy to get */``      ``set_bit_no = xor2 & ~(xor2-``1``);``     ` `      ``/* Now divide elements in two sets:``            ``1) The elements having the``               ``corresponding bit as 1.``            ``2) The elements having the``               ``corresponding bit as 0.  */``      ``for``(i = ``0``; i < size; i++)``      ``{``         ``/* XOR of first set is finally going``            ``to hold one odd occurring number x */``        ``if``((arr[i] & set_bit_no)>``0``)``          ``x = x ^ arr[i];``     ` `         ``/* XOR of second set is finally going``            ``to hold the other odd occurring number y */``        ``else``          ``y = y ^ arr[i];``      ``}``     ` `      ``System.out.println(``"The two ODD elements are "``+``                                        ``x + ``" & "` `+ y);``    ``}``    ` `    ``// main function``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `arr[] = {``4``, ``2``, ``4``, ``5``, ``2``, ``3``, ``3``, ``1``};``        ``int` `arr_size = arr.length;``        ``printTwoOdd(arr, arr_size);``    ``}``}`

## Python3

 `# Python3 program to find the``# two odd occurring elements` `# Prints two numbers that occur odd``# number of times. The function assumes``# that the array size is at least 2 and``# there are exactly two numbers occurring``# odd number of times.``def` `printTwoOdd(arr, size):``    ` `    ``# Will hold XOR of two odd occurring elements``    ``xor2 ``=` `arr[``0``]``    ` `    ``# Will have only single set bit of xor2``    ``set_bit_no ``=` `0` `    ``n ``=` `size ``-` `2``    ``x, y ``=` `0``, ``0` `    ``# Get the xor of all elements in arr[].``    ``# The xor will basically be xor of two``    ``# odd occurring elements``    ``for` `i ``in` `range``(``1``, size):``        ``xor2 ``=` `xor2 ^ arr[i]` `    ``# Get one set bit in the xor2. We get``    ``# rightmost set bit in the following``    ``# line as it is easy to get``    ``set_bit_no ``=` `xor2 & ~(xor2 ``-` `1``)` `    ``# Now divide elements in two sets:``    ``# 1) The elements having the corresponding bit as 1.``    ``# 2) The elements having the corresponding bit as 0.``    ``for` `i ``in` `range``(size):``    ` `        ``# XOR of first set is finally going to ``        ``# hold one odd  occurring number x``        ``if``(arr[i] & set_bit_no):``            ``x ``=` `x ^ arr[i]` `        ``# XOR of second set is finally going``        ``# to hold the other odd occurring number y``        ``else``:``            ``y ``=` `y ^ arr[i]` `    ``print``(``"The two ODD elements are"``, x, ``"&"``, y)` `# Driver Code``arr ``=` `[``4``, ``2``, ``4``, ``5``, ``2``, ``3``, ``3``, ``1``]``arr_size ``=` `len``(arr)``printTwoOdd(arr, arr_size)` `# This code is contributed by Anant Agarwal.`

## C#

 `// C# program to find two odd``// occurring elements``using` `System;` `class` `main``{  ``      ` `    ``// Prints two numbers that occur``    ``// odd number of times. Function``    ``// assumes that array size is at``    ``// least 2 and there are exactly``    ``// two numbers occurring odd number``    ``// of times.``    ``static` `void` `printTwoOdd(``int` `[]arr, ``int` `size) {``   ` `      ``// Will hold XOR of two odd``      ``//occurring elements  ``      ``int` `xor2 = arr;``       ` `      ``// Will have only single set``      ``// bit of xor2``      ``int` `set_bit_no; ``      ``int` `i;``      ` `      ``//int n = size - 2;``      ``int` `x = 0, y = 0;``      ` `      ``// Get the xor of all the elements``      ``// in arr[].The xor will basically``      ``// be xor of two odd occurring``      ``// elements``      ``for``(i = 1; i < size; i++)``        ``xor2 = xor2 ^ arr[i];``      ` `      ``// Get one set bit in the xor2.``      ``// We get rightmost set bit in``      ``// the following line as it is``      ``// to get.``         ``set_bit_no = xor2 & ~(xor2-1);``      ` `      ``// divide elements in two sets:``      ``// 1) The elements having the``      ``// corresponding bit as 1.``      ``// 2) The elements having the``      ``// corresponding bit as 0.``      ``for``(i = 0; i < size; i++)``      ``{``         ``// XOR of first set is finally``         ``// going to hold one odd``         ``// occurring number x``            ``if``((arr[i] & set_bit_no)>0)``            ``x = x ^ arr[i];``      ` `            ``// XOR of second set is finally``            ``// going to hold the other``            ``// odd occurring number y``            ``else``            ``y = y ^ arr[i];``      ``}``      ` `      ``Console.WriteLine(``"The two ODD elements are "``+``                                        ``x + ``" & "` `+ y);``    ``}``     ` `    ``// main function``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]arr = {4, 2, 4, 5, 2, 3, 3, 1};``        ``int` `arr_size = arr.Length;``        ``printTwoOdd(arr, arr_size);``    ``}``}` `//This code is contributed by Anant Agarwal.`

## PHP

 ``

## Javascript

 ``
Output
`The two ODD elements are 5 & 1`

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

Another solution would be using map O(n) time and O(n) extra space solution :

The extra space O(n) can be minimized to O(1) by directly taking inputs in map instead of array.

The idea is explained below using comments in code-

## C++

 `// C++ Program to find the two odd occurring elements``#include ``using` `namespace` `std;` `/* Prints two numbers that occur odd number of times. The``function assumes that the array size is at least 2 and``there are exactly two numbers occurring odd number of times.``*/` `void` `printTwoOdd(``int` `arr[], ``int` `size)``{``    ``/*Create map and calculate frequency of array of``     ``* elements using array.*/` `    ``unordered_map<``int``, ``int``> m;``    ``for` `(``int` `i = 0; i < size; i++) {``        ``m[arr[i]]++;``    ``}` `    ``/*Traverse through the map and check if its second``      ``element that is the frequency is odd or not.Then this``      ``is the odd occurring element .Its is clearly mentioned``      ``in problem that there are only two odd occurring``      ``elements so this will print those two elements.*/` `    ``cout << ``"The two ODD elements are "``;``    ``for` `(``auto``& x : m) {``        ``if` `(x.second % 2 != 0)``            ``cout << x.first << ``", "``;``    ``}``}` `/* Driver code */``int` `main()``{``    ``int` `arr[] = { 4, 2, 4, 5, 2, 3, 3, 1 };``    ``int` `arr_size = ``sizeof``(arr) / ``sizeof``(arr);``    ``printTwoOdd(arr, arr_size);``    ``return` `0;``}` `// This is code is contributed by Abhishek`

## Python3

 `# Python3 program to find the two odd occurring elements` `""" Prints two numbers that occur odd number of times.``The function assumes that the array size is at least 2``and there are exactly two numbers occurring odd number``of times.``"""``def` `printTwoOdd(arr, size):``    ``arr.sort()``    ` `    ``# Create map and calculate frequency of array``    ``# of elements using array.``    ``m ``=` `{}``    ``for` `i ``in` `range``(size):``        ``if` `arr[i] ``not` `in` `m:``            ``m[arr[i]] ``=` `0``            ` `        ``m[arr[i]] ``+``=` `1``    ` `    ``"""Traverse through the map and check if its second``    ``element that is the frequency is odd or not.Then this``    ``is the odd occurring element .Its is clearly mentioned``    ``in problem that there are only two odd occurring``    ``elements so this will prthose two elements."""``    ``print``(``"The two ODD elements are "``, end ``=` `"")``    ``for` `x ``in` `m:``        ``if` `(m[x] ``%` `2` `!``=` `0``):``            ``print``(x, end ``=` `", "``)``            ` `# Driver code``arr ``=` `[ ``4``, ``2``, ``4``, ``5``, ``2``, ``3``, ``3``, ``1` `]``arr_size ``=` `len``(arr)` `printTwoOdd(arr, arr_size)` `# This code is contributed by shubhamsingh10`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;``using` `System.Linq;` `public` `class` `GFG {``    ` `/* Prints two numbers that occur odd number of times. The``function assumes that the array size is at least 2 and``there are exactly two numbers occurring odd number of times.``*/``static` `void` `printTwoOdd(``int``[] arr, ``int` `size)``{``    ``/*Create map and calculate frequency of array of``     ``* elements using array.*/` `    ``Dictionary<``int``, ``int``> m = ``new` `Dictionary<``int``, ``int``>();``     ``for` `(``int` `i = 0; i < size; i++)``        ``{``            ``if` `(m.ContainsKey(arr[i]))``                ``m[arr[i]]++;``            ``else``                ``m.Add(arr[i], 1);``        ``}` `    ``/*Traverse through the map and check if its second``      ``element that is the frequency is odd or not.Then this``      ``is the odd occurring element .Its is clearly mentioned``      ``in problem that there are only two odd occurring``      ``elements so this will print those two elements.*/` `    ``Console.Write(``"The two ODD elements are "``);``    ``foreach` `(``int` `x ``in` `m.Keys.ToList()){``        ``if` `(m[x] % 2 != 0)``            ``Console.Write(x + ``", "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main (``string``[] args) {``    ` `    ``int``[] arr = { 4, 2, 4, 5, 2, 3, 3, 1 };``    ``int` `arr_size = arr.Length;``    ``printTwoOdd(arr, arr_size);``}``}` `//  This code is contributed by splevel62.`

## Javascript

 ``
Output
`The two ODD elements are 1, 5, `

Time Complexity: O(n)

Auxiliary Space: O(n)