Find the two numbers with odd occurrences in an unsorted array

Given an unsorted array in which other than two numbers all other numbers occur even number of times. This function will find the two numbers that occur odd number of times.

Note: The array should contain exactly two numbers that occur odd number of times.

Example

INPUT:
arr[] = {5, 5, 4, 6, 6, 7}
OUTPUT:
4 and 7 are the numbers that occur odd number of times

Time Complexity: O(n)

The main idea is using bitwise XOR.
Example, XOR of 4 and 7 is
4(100) ^ 7(111) = 011

Properties of XOR:

i) XOR of any number with itself gives 0 ie, x ^ x = 0
ii) XOR of any number with 0 gives us the number ie, x ^ 0 = x

Algorithm

1. When we do XOR of all numbers in the array, we will get XOR of the numbers with odd occurrences because the XOR of the numbers with even occurrences will be 0.

Suppose lets take the above example, XOR of 5, 5 will be 0 and XOR of 6, 6  will be 0. We will remain with XOR of 4, 7 which is 011.

2. After getting the XOR value, we will divide the numbers in the array into two groups using a set bit(bit having value 1) of XOR value ie, arr[i] & set_bit . set bit can be any bit, but it is easy to get right most set bit .   

3. Both the odd occurring numbers will go into each group.

4. When we do XOR of all elements in each group, we will get the two odd occurring numbers.

Implentation on above Algorithm:

arr[] = {5, 5, 4, 6, 6, 7}
XOR(^) of all the numbers in the array is
xor_val = 5(101) ^  5(101) ^  4(100) ^  6(111) ^  6(110) ^  7(111) = 3(011), which is nothing but XOR of 4 and 7.

Finding the set_bit
xor_val & ~(xor_val -1)
= 3(011) & ~(2(010))
= 3(011) & 5(101)
= 1(001)

Finding the two numbers.
Initialize num1 =0, num2 =0
Dividing the elements into two groups by using IF condition and doing XOR(^) of all elements in one group

IF(5(101) & 1(001)) ie, ( 5(101) & 1(001) )= 1(001)
So,
num1 = 0(000) ^ 5(101)
num1 = 5(101)

IF(5(101) & 1(001)) ie, ( 5(101) & 1(001) )= 1(001)
So,
num1 = 5(101) ^ 5(101)
num1 = 0(000)

IF(4(100) & 1(001)) ie, ( 4(100) & 1(001) )= 0(000)
So,
num2 = 0(000) ^ 4(100)
num2 = 4(100)

IF(6(110) & 1(001)) ie, ( 6(110) & 1(001) )= 0(000)
So,
num2 = 4(100) ^ 6(110)
num2 = 2(010)

IF(6(110) & 1(001)) ie, ( 6(110) & 1(001) )= 0(000)
So,
num2 = 2(010) ^ 6(110)
num2 = 4(100)

IF(7(111) & 1(001)) ie, ( 7(111) & 1(001) )= 7(111)
So,
num1 = 0(000) ^ 7(111)
num1 = 7(111)

Therfore, the two numbers are num1 and num2 ie, 4 and 7

C++ Program

#include <bits/stdc++.h>
using namespace std;

void oddOccurrences(int arr[], int n) //prints the odd occurrences numbers
{
	int xor_val = arr[0]; 
	int setbit = 0;
	for (int i = 0; i < n; ++i) //XOR of all numbers in the array
	{
		xor_val = xor_val ^ arr[i];
	}
	setbit = xor_val & ~(xor_val -1); //Finding the right most set bit of xor_val
	int num1 = 0, num2 =0;
	for (int i = 0; i < n; ++i)
	{
		if(arr[i] & setbit)
		{
			num1 = num1 ^ arr[i]; //XOR of all the elements in one group
		}
		else
		{
			num2 = num2 ^ arr[i]; //XOR of all the elements in other group
		}
	}
	cout<<"The odd occurrences in the array are "<<num1<<", "<<num2<<endl;

}
int main()
{
	int arr[] = {5, 5, 4,6, 6, 7};
	int n = sizeof(arr)/sizeof(arr[0]);
	oddOccurrences(arr,n);

}
Try It


Next > < Prev
Scroll to Top