Find the two numbers with odd occurrences in an unsorted array

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

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.


arr[] = {5, 5, 4, 6, 6, 7}
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


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)
num1 = 0(000) ^ 5(101)
num1 = 5(101)

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

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

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

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

IF(7(111) & 1(001)) ie, ( 7(111) & 1(001) )= 7(111)
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
			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]);

Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top