Find the Number Occurring Odd Number of Times using Bit Manipulation

The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring elements. 

Here ^ is the XOR operators;
Note :
x^0 = x
x^y=y^x (Commutative property holds)
(x^y)^z = x^(y^z) (Distributive property holds)
x^x=0

Below is the implementation of the above approach.  

C++
// C++ program to find the element
// occurring odd number of times
#include <bits/stdc++.h>
using namespace std;

// Function to find element occurring
// odd number of times
int getOddOccurrence(int ar[], int ar_size)
{
    int res = 0; 
    for (int i = 0; i < ar_size; i++)     
        res = res ^ ar[i];
    
    return res;
}

/* Driver function to test above function */
int main()
{
    int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
    int n = sizeof(ar)/sizeof(ar[0]);
    
    // Function calling
    cout << getOddOccurrence(ar, n);
    
    return 0;
}
C
    
// C program to find the element
// occurring odd number of times
#include <stdio.h>

// Function to find element occurring
// odd number of times
int getOddOccurrence(int ar[], int ar_size)
{
    int res = 0; 
    for (int i = 0; i < ar_size; i++)     
        res = res ^ ar[i];
    
    return res;
}

/* Driver function to test above function */
int main()
{
    int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
    int n = sizeof(ar) / sizeof(ar[0]);
    
    // Function calling
    printf("%d", getOddOccurrence(ar, n));
    return 0;
}
Java
//Java program to find the element occurring odd number of times

class OddOccurrence 
{
    int getOddOccurrence(int ar[], int ar_size) 
    {
        int i;
        int res = 0;
        for (i = 0; i < ar_size; i++) 
        {
            res = res ^ ar[i];
        }
        return res;
    }

    public static void main(String[] args) 
    {
        OddOccurrence occur = new OddOccurrence();
        int ar[] = new int[]{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
        int n = ar.length;
        System.out.println(occur.getOddOccurrence(ar, n));
    }
}
// This code has been contributed by Mayank Jaiswal
Python
    
# Python program to find the element occurring odd number of times

def getOddOccurrence(arr):

    # Initialize result
    res = 0
    
    # Traverse the array
    for element in arr:
        # XOR with the result
        res = res ^ element

    return res

# Test array
arr = [ 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2]

print("%d" % getOddOccurrence(arr))
C#
// C# program to find the element
// occurring odd number of times
using System;

class GFG
{
    // Function to find the element 
    // occurring odd number of times
    static int getOddOccurrence(int []arr, int arr_size)
    {
        int res = 0;
        for (int i = 0; i < arr_size; i++) 
        {
            res = res ^ arr[i];
        }
        return res;
    }
    
    // Driver code
    public static void Main()
    {
        int []arr = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 };
        int n = arr.Length;
        Console.Write(getOddOccurrence(arr, n));
    }
}

// This code is contributed by Sam007
Javascript
// JavaScript program to find the element 
// occurring odd number of times 

// Function to find the element 
// occurring odd number of times 
function getOddOccurrence( ar, ar_size)
{
    let res = 0; 
    
    for (let i = 0; i < ar_size; i++)     
        res = res ^ ar[i];
    
    return res;
}

    // driver code 

    let arr = [ 2, 3, 5, 4, 5, 2, 4, 
                3, 5, 2, 4, 4, 2 ]; 
    let n = arr.length; 

    // Function calling 
    console.log(getOddOccurrence(arr, n)); 
PHP
<?php
// PHP program to find the 
// element occurring odd 
// number of times

// Function to find element 
// occurring odd number of times
function getOddOccurrence(&$ar, $ar_size)
{
    $res = 0; 
    for ($i = 0; $i < $ar_size; $i++)     
        $res = $res ^ $ar[$i];
    
    return $res;
}

// Driver Code
$ar = array(2, 3, 5, 4, 5, 2,
            4, 3, 5, 2, 4, 4, 2);
$n = sizeof($ar);

// Function calling
echo(getOddOccurrence($ar, $n));
    
// This code is contributed 
// by Shivi_Aggarwal
?>

Output
5

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



Find the Number Occurring Odd Number of Times

Given an array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space.

Examples : 

Input : arr = {1, 2, 3, 2, 3, 1, 3}
Output : 3

Input : arr = {5, 7, 2, 7, 5, 2, 5}
Output : 5

Recommended Practice

Similar Reads

Find the Number Occurring Odd Number of Times using Nested Loop:

A Simple Solution is to run two nested loops. The outer loop picks all elements one by one and the inner loop counts the number of occurrences of the element picked by the outer loop....

Find the Number Occurring Odd Number of Times using Hashing:

A Better Solution is to use Hashing. Use array elements as a key and their counts as values. Create an empty hash table. One by one traverse the given array elements and store counts....

Find the Number Occurring Odd Number of Times using Bit Manipulation:

The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring elements....

Contact Us