Bitwise XOR of elements having odd frequency
Given an array arr[] of N elements, the task is to find the XOR of the elements which appear an odd number of times in the array.
Examples:
Input: arr[] = {1, 2, 1, 3, 3, 4, 2, 3, 1}
Output: 6
Elements with odd frequencies are 1, 3 and 4.
And (1 ^ 3 ^ 4) = 6
Input: arr[] = {2, 2, 7, 8, 7}
Output: 8
Naive Approach: Traverse the array and store the frequencies of all the elements in a unordered_map. Now, calculate the XOR of elements having odd frequency using the map created in the previous step.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int xorOdd( int arr[], int n)
{
unordered_map< int , int > m;
for ( int i = 0; i < n; i++)
m[arr[i]]++;
int xorArr = 0;
for ( auto it = m.begin(); it != m.end(); it++) {
if ((it->second) & 1) {
xorArr ^= it->first;
}
}
return xorArr;
}
int main()
{
int arr[] = { 1, 2, 1, 3, 3, 4, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << xorOdd(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int xorOdd( int arr[], int n)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
int xorArr = 0 ;
for (Map.Entry<Integer,
Integer> it : mp.entrySet())
{
if (((it.getValue()) % 2 ) == 1 )
{
xorArr ^= it.getKey();
}
}
return xorArr;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 3 , 3 , 4 , 2 , 3 , 1 };
int n = arr.length;
System.out.println(xorOdd(arr, n));
}
}
|
Python3
def xorOdd(arr, n) :
m = dict .fromkeys(arr, 0 );
for i in range (n) :
m[arr[i]] + = 1 ;
xorArr = 0 ;
for key,value in m.items() :
if (value & 1 ) :
xorArr ^ = key;
return xorArr;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 1 , 3 , 3 , 4 , 2 , 3 , 1 ];
n = len (arr);
print (xorOdd(arr, n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int xorOdd( int []arr, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0 ; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
int xorArr = 0;
foreach (KeyValuePair< int , int > it in mp)
{
if (((it.Value) % 2) == 1)
{
xorArr ^= it.Key;
}
}
return xorArr;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 1, 3, 3, 4, 2, 3, 1 };
int n = arr.Length;
Console.WriteLine(xorOdd(arr, n));
}
}
|
Javascript
<script>
function xorOdd(arr, n)
{
let mp = new Map();
for (let i = 0 ; i < n; i++)
{
if (mp.has(arr[i]))
{
mp.set(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.set(arr[i], 1);
}
}
let xorArr = 0;
for (let [key, value] of mp.entries())
{
if (((value) % 2) == 1)
{
xorArr ^= key;
}
}
return xorArr;
}
let arr = [ 1, 2, 1, 3, 3, 4, 2, 3, 1 ];
let n = arr.length;
document.write(xorOdd(arr, n));
</script>
|
This solution takes O(n) time and O(n) space.
Efficient Approach:
This approach uses two important properties of XOR – a ^ a = 0 and 0 ^ a = a. Take XOR of all the elements in the array. The result will be the XOR of numbers that appears an odd number of times since elements appearing even number of times eventually cancel out each other.
C++
#include<bits/stdc++.h>
using namespace std;
int xorOdd( int arr[], int n) {
int result = 0;
for ( int i = 0; i < n; ++i) {
result ^= arr[i];
}
return result;
}
int main() {
int arr[] = { 1, 2, 1, 3, 3, 4, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << xorOdd(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int xorOdd( int arr[], int n)
{
int result = 0 ;
for ( int i = 0 ; i < n; ++i)
{
result ^= arr[i];
}
return result;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 1 , 3 , 3 ,
4 , 2 , 3 , 1 };
int n = arr.length;
System.out.println(xorOdd(arr, n));
}
}
|
Python3
def xorOdd(arr, n):
result = 0
for i in range (n):
result ^ = arr[i]
return result
if __name__ = = "__main__" :
arr = [ 1 , 2 , 1 , 3 , 3 ,
4 , 2 , 3 , 1 ]
n = len (arr)
print ( xorOdd(arr, n))
|
C#
using System;
class GFG {
static int xorOdd( int [] arr, int n)
{
int result = 0;
for ( int i = 0; i < n; ++i) {
result ^= arr[i];
}
return result;
}
public static void Main()
{
int [] arr = { 1, 2, 1, 3, 3, 4, 2, 3, 1 };
int n = arr.Length;
Console.Write(xorOdd(arr, n));
}
}
|
Javascript
<script>
function xorOdd(arr, n) {
let result = 0;
for (let i = 0; i < n; ++i) {
result ^= arr[i];
}
return result;
}
let arr = [ 1, 2, 1, 3, 3, 4, 2, 3, 1 ];
let n = arr.length;
document.write(xorOdd(arr, n));
</script>
|
This solution takes O(n) time and O(1) space.
Last Updated :
01 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...