Two odd occurring elements in an array where all other occur even times

Given an array where all elements appear even number of times except two, print the two odd occurring elements. It may be assumed that the size of array is at-least two.

Examples:

Input : arr[] = {2, 3, 8, 4, 4, 3, 7, 8}
Output : 2 7

Input : arr[] = {15, 10, 10, 50 7, 5, 5, 50, 50, 50, 50, 50}
Output : 7 15

A simple solution is to use two nested loops. The outer loop traverses through all elements. The inner loop counts occurrences of the current element. We print the elements whose counts of occurrences are odd. Time complexity if this solution is O(n2)

A better solution is to use hashing. Time complexity of this solution is O(n) but it requires extra space.

An efficient solution is to use bitwise operators. The idea is based on approach used in two missing elements and two repeating elements.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to find two odd occurring elements
// in an array where all other elements appear
// even number of times.
#include <bits/stdc++.h>
using namespace std;
  
void printOdds(int arr[], int n)
{
    // Find XOR of all numbers
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
  
    // Find a set bit in the XOR (We find
    // rightmost set bit here)
    int set_bit = res & (~(res - 1));
  
    // Traverse through all numbers and
    // divide them in two groups 
    // (i) Having set bit set at same 
    //     position as the only set bit 
    //     in set_bit
    // (ii) Having 0 bit at same position
    //      as the only set bit in set_bit
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] & set_bit)
            x = x ^ arr[i];
        else
            y = y ^ arr[i];
    }
  
    // XOR of two different sets are our
    // required numbers.
    cout << x << " " << y;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 3, 3, 4, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printOdds(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to find two 
// odd occurring elements
// in an array where all
// other elements appear
// even number of times.
  
class GFG
{
static void printOdds(int arr[], 
                      int n)
{
    // Find XOR of
    // all numbers
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
  
    // Find a set bit in the 
    // XOR (We find rightmost 
    // set bit here)
    int set_bit = res & 
                  (~(res - 1));
  
    // Traverse through all 
    // numbers and divide them 
    // in two groups (i) Having 
    // set bit set at same position 
    // as the only set bit in 
    // set_bit (ii) Having 0 bit at
    // same position as the only 
    // set bit in set_bit
    int x = 0, y = 0;
    for (int i = 0; i < n; i++)
    {
        if ((arr[i] & set_bit) != 0)
            x = x ^ arr[i];
        else
            y = y ^ arr[i];
    }
  
    // XOR of two different 
    // sets are our required
    // numbers.
    System.out.println( x + " " + y);
}
  
// Driver code
public static void main(String [] args)
{
    int arr[] = { 2, 3, 3
                  4, 4, 5 };
    int n = arr.length;
    printOdds(arr, n);
}
}
  
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to find two 
# odd occurring elements in
# an array where all other 
# elements appear even number
# of times.
def printOdds(arr, n):
  
    # Find XOR of all numbers
    res = 0
    for i in range(0, n):
        res = res ^ arr[i]
  
    # Find a set bit in 
    # the XOR (We find 
    # rightmost set bit here)
    set_bit = res & (~(res - 1))
  
    # Traverse through all numbers 
    # and divide them in two groups 
    # (i) Having set bit set at 
    # same position as the only set 
    # bit in set_bit
    # (ii) Having 0 bit at same 
    # position as the only set
    # bit in set_bit
    x = 0
    y = 0
    for i in range(0, n):
        if (arr[i] & set_bit):
            x = x ^ arr[i]
        else:
            y = y ^ arr[i]
      
    # XOR of two different
    # sets are our
    # required numbers.
    print(x , y, end = "")
  
# Driver code
arr = [2, 3, 3, 4, 4, 5 ]
n = len(arr) 
printOdds(arr, n)
  
# This code is contributed
# by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to find two 
// odd occurring elements
// in an array where all
// other elements appear
// even number of times.
using System;
  
class GFG
{
static void printOdds(int []arr, 
                      int n)
{
    // Find XOR of
    // all numbers
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
  
    // Find a set bit in the 
    // XOR (We find rightmost 
    // set bit here)
    int set_bit = res & 
               (~(res - 1));
  
    // Traverse through all 
    // numbers and divide them 
    // in two groups (i) Having 
    // set bit set at same position 
    // as the only set bit in 
    // set_bit (ii) Having 0 bit at
    // same position as the only 
    // set bit in set_bit
    int x = 0, y = 0;
    for (int i = 0; i < n; i++)
    {
        if ((arr[i] & set_bit) != 0)
            x = x ^ arr[i];
        else
            y = y ^ arr[i];
    }
  
    // XOR of two different 
    // sets are our required
    // numbers.
    Console.WriteLine(x + " " + y);
}
  
// Driver code
public static void Main()
{
    int []arr = { 2, 3, 3, 
                  4, 4, 5 };
    int n = arr.Length;
    printOdds(arr, n);
}
}
  
// This code is contributed by
// Akanksha Rai(Abby_akku)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to find two odd 
// occurring elements in an 
// array where all other elements 
// appear even number of times.
function printOdds($arr, $n)
{
    // Find XOR of all numbers
    $res = 0;
    for ($i = 0; $i < $n; $i++)
        $res = $res ^ $arr[$i];
  
    // Find a set bit in the 
    // XOR (We find rightmost
    // set bit here)
    $set_bit = $res & (~($res - 1));
  
    // Traverse through all numbers
    // and divide them in two groups 
    // (i) Having set bit set at same 
    // position as the only set bit 
    // in set_bit
    // (ii) Having 0 bit at same position
    // as the only set bit in set_bit
    $x = 0;
    $y = 0;
    for ($i = 0; $i < $n; $i++)
    {
        if ($arr[$i] & $set_bit)
            $x = $x ^ $arr[$i];
        else
            $y = $y ^ $arr[$i];
    }
  
    // XOR of two different sets
    // are our required numbers.
    echo($x . " " . $y);
}
  
// Driver code
$arr = array( 2, 3, 3, 4, 4, 5 );
$n = sizeof($arr);
printOdds($arr, $n);
  
// This code is contributed by Smitha
?>

chevron_right


Output:

5 2

Time Complexity : O(n)
Auxiliary Space : O(1)



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 :


1


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