Given an array that contains odd number of occurrences for all numbers except for a few elements which are present even number of times. Find the elements which have even occurrences in the array in O(n) time complexity and O(1) extra space.
Assume array contain elements in the range 0 to 63.
Examples :
Input: [9, 12, 23, 10, 12, 12, 15, 23, 14, 12, 15]
Output: 12, 23 and 15
Input: [1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3]
Output: 4 and 7
Input: [4, 4, 10, 10, 4, 4, 4, 4, 10, 10]
Output: 4 and 10
A simple method would be to traverse the array and store frequencies of its elements in a map. Later, print elements that have even count in the map. The solution takes O(n) time but requires extra space for storing frequencies. Below is an interesting method to solve this problem using bitwise operators.
This method assumes that long long integers are stored using 64 bits. The idea is to use XOR operator. We know that
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
Consider below input –
1, 4, 7, 5, 9, 7, 3, 4, 6, 8, 3, 0, 3
If we left-shift 1 by value of each element of the array and take XOR of all the items, we will get below binary integer –
1101101011
Each 1 in the i’th index from the right represents odd occurrence of element i. And each 0 in the i’th index from the right represents even or non-occurrence of element i in the array.
0 is present in 2nd, 4th and 7th position in above binary number. But 2 is not present in our array. So our answer is 4 and 7.
Below is the implementation of above idea
C++
#include <iostream>
using namespace std;
void printRepeatingEven( int arr[], int n)
{
long long _xor = 0L;
long long pos;
for ( int i = 0; i < n; ++i)
{
pos = 1 << arr[i];
_xor ^= pos;
}
for ( int i = 0; i < n; ++i)
{
pos = 1 << arr[i];
if (!(pos & _xor))
{
cout << arr[i] << " " ;
_xor ^= pos;
}
}
}
int main()
{
int arr[] = { 9, 12, 23, 10, 12, 12, 15, 23,
14, 12, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
printRepeatingEven(arr, n);
return 0;
}
|
Java
class GFG
{
static void printRepeatingEven( int arr[],
int n)
{
long _xor = 0L;
long pos;
for ( int i = 0 ; i < n; ++i)
{
pos = 1 << arr[i];
_xor ^= pos;
}
for ( int i = 0 ; i < n; ++i)
{
pos = 1 << arr[i];
if (!((pos & _xor)!= 0 ))
{
System.out.print(arr[i] + " " );
_xor ^= pos;
}
}
}
public static void main(String args[])
{
int arr[] = { 9 , 12 , 23 , 10 , 12 , 12 ,
15 , 23 , 14 , 12 , 15 };
int n = arr.length;
printRepeatingEven(arr, n);
}
}
|
C#
using System;
class GFG
{
static void printRepeatingEven( int [] arr,
int n)
{
long _xor = 0L;
long pos;
for ( int i = 0; i < n; ++i)
{
pos = 1 << arr[i];
_xor ^= pos;
}
for ( int i = 0; i < n; ++i)
{
pos = 1 << arr[i];
if (!((pos & _xor) != 0))
{
Console.Write(arr[i] + " " );
_xor ^= pos;
}
}
}
public static void Main()
{
int [] arr = {9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15};
int n = arr.Length;
printRepeatingEven(arr, n);
}
}
|
PHP
<?php
function printRepeatingEven( $arr , $n )
{
$_xor = 0;
$pos ;
for ( $i = 0; $i < $n ; ++ $i )
{
$pos = 1 << $arr [ $i ];
$_xor ^= $pos ;
}
for ( $i = 0; $i < $n ; ++ $i )
{
$pos = 1 << $arr [ $i ];
if (!( $pos & $_xor ))
{
echo $arr [ $i ], " " ;
$_xor ^= $pos ;
}
}
}
$arr = array (9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15 );
$n = sizeof( $arr );
printRepeatingEven( $arr , $n );
?>
|
Python 3
def printRepeatingEven(arr, n):
axor = 0 ;
for i in range ( 0 , n):
pos = 1 << arr[i];
axor ^ = pos;
for i in range ( 0 , n - 1 ):
pos = 1 << arr[i];
if ( not (pos & axor)):
print (arr[i], end = " " );
axor ^ = pos;
arr = [ 9 , 12 , 23 , 10 , 12 , 12 ,
15 , 23 , 14 , 12 , 15 ];
n = len (arr) ;
printRepeatingEven(arr, n);
|
Javascript
<script>
function printRepeatingEven(arr, n)
{
let _xor = 0;
let pos;
for (let i = 0; i < n; ++i)
{
pos = 1 << arr[i];
_xor ^= pos;
}
for (let i = 0; i < n; ++i)
{
pos = 1 << arr[i];
if (!((pos & _xor)!=0))
{
document.write(arr[i] + " " );
_xor ^= pos;
}
}
}
let arr = [9, 12, 23, 10, 12, 12,
15, 23, 14, 12, 15];
let n = arr.length;
printRepeatingEven(arr, n);
</script>
|
Output :
12 23 15
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above