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 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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the xor of
// elements having odd frequency
int xorOdd(int arr[], int n)
{
    // To store the frequency
    // of all the elements
    unordered_map<int, int> m;
  
    // Update the map with the
    // frequency of the elements
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
  
    // To store the XOR of the elements
    // appearing odd number of
    // times in the array
    int xorArr = 0;
  
    // Traverse the map using an iterator
    for (auto it = m.begin(); it != m.end(); it++) {
  
        // Check for odd frequency
        // and update the xor
        if ((it->second) & 1) {
            xorArr ^= it->first;
        }
    }
  
    return xorArr;
}
  
// Driver code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
// Function to return the xor of
// elements having odd frequency
static int xorOdd(int arr[], int n)
{
    // To store the frequency
    // of all the elements
    HashMap<Integer, 
            Integer> mp = new HashMap<Integer,
                                      Integer>();
  
    // Update the map with the
    // frequency of the elements
    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);
        }
    }
      
    // To store the XOR of the elements
    // appearing odd number of
    // times in the array
    int xorArr = 0;
  
    // Traverse the map using an iterator
    for (Map.Entry<Integer,
                   Integer> it : mp.entrySet()) 
    {
        // Check for odd frequency
        // and update the xor
        if (((it.getValue()) % 2) ==1)
        {
            xorArr ^= it.getKey();
        }
    }
    return xorArr;
}
  
// Driver code
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));
}
}
  
// This code contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the xor of 
# elements having odd frequency 
def xorOdd(arr, n) : 
  
    # To store the frequency 
    # of all the elements 
    m = dict.fromkeys(arr, 0); 
  
    # Update the map with the 
    # frequency of the elements 
    for i in range(n) :
        m[arr[i]] += 1
  
    # To store the XOR of the elements 
    # appearing odd number of 
    # times in the array 
    xorArr = 0
  
    # Traverse the map using an iterator 
    for key,value in m.items() :
  
        # Check for odd frequency 
        # and update the xor 
        if (value & 1) :
            xorArr ^= key; 
  
    return xorArr; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 1, 2, 1, 3, 3, 4, 2, 3, 1 ]; 
    n = len(arr); 
  
    print(xorOdd(arr, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;                 
      
class GFG
{
      
// Function to return the xor of
// elements having odd frequency
static int xorOdd(int []arr, int n)
{
    // To store the frequency
    // of all the elements
    Dictionary<int
               int> mp = new Dictionary<int
                                        int>();
  
    // Update the map with the
    // frequency of the elements
    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);
        }
    }
      
    // To store the XOR of the elements
    // appearing odd number of
    // times in the array
    int xorArr = 0;
  
    // Traverse the map using an iterator
    foreach(KeyValuePair<int, int> it in mp) 
    {
        // Check for odd frequency
        // and update the xor
        if (((it.Value) % 2) == 1)
        {
            xorArr ^= it.Key;
        }
    }
    return xorArr;
}
  
// Driver code
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));
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.