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
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.
Below is the implementation of the brute force approach :
C++
#include<bits/stdc++.h>
using namespace std;
int getOddOccurrence( int arr[], int arr_size)
{
for ( int i = 0; i < arr_size; i++) {
int count = 0;
for ( int j = 0; j < arr_size; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count % 2 != 0)
return arr[i];
}
return -1;
}
int main()
{
int arr[] = { 2, 3, 5, 4, 5, 2,
4, 3, 5, 2, 4, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getOddOccurrence(arr, n);
return 0;
}
|
Java
class OddOccurrence {
static int getOddOccurrence( int arr[], int arr_size)
{
int i;
for (i = 0 ; i < arr_size; i++) {
int count = 0 ;
for ( int j = 0 ; j < arr_size; j++) {
if (arr[i] == arr[j])
count++;
}
if (count % 2 != 0 )
return arr[i];
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = new int []{ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 };
int n = arr.length;
System.out.println(getOddOccurrence(arr, n));
}
}
|
Python3
def getOddOccurrence(arr, arr_size):
for i in range ( 0 ,arr_size):
count = 0
for j in range ( 0 , arr_size):
if arr[i] = = arr[j]:
count + = 1
if (count % 2 ! = 0 ):
return arr[i]
return - 1
arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ]
n = len (arr)
print (getOddOccurrence(arr, n))
|
C#
using System;
class GFG
{
static int getOddOccurrence( int []arr, int arr_size)
{
for ( int i = 0; i < arr_size; i++) {
int count = 0;
for ( int j = 0; j < arr_size; j++) {
if (arr[i] == arr[j])
count++;
}
if (count % 2 != 0)
return arr[i];
}
return -1;
}
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));
}
}
|
Javascript
<script>
function getOddOccurrence(arr, arr_size)
{
for (let i = 0; i < arr_size; i++) {
let count = 0;
for (let j = 0; j < arr_size; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count % 2 != 0)
return arr[i];
}
return -1;
}
let arr = [ 2, 3, 5, 4, 5, 2,
4, 3, 5, 2, 4, 4, 2 ];
let n = arr.length;
document.write(getOddOccurrence(arr, n));
</script>
|
PHP
<?php
function getOddOccurrence(& $arr , $arr_size )
{
$count = 0;
for ( $i = 0;
$i < $arr_size ; $i ++)
{
for ( $j = 0;
$j < $arr_size ; $j ++)
{
if ( $arr [ $i ] == $arr [ $j ])
$count ++;
}
if ( $count % 2 != 0)
return $arr [ $i ];
}
return -1;
}
$arr = array (2, 3, 5, 4, 5, 2,
4, 3, 5, 2, 4, 4, 2);
$n = sizeof( $arr );
echo (getOddOccurrence( $arr , $n ));
?>
|
Output :
5
Time Complexity: O(n^2)
Auxiliary Space: O(1)
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.
Below is the implementation of the above approch:
C++
#include <bits/stdc++.h>
using namespace std;
int getOddOccurrence( int arr[], int size)
{
unordered_map< int , int > hash;
for ( int i = 0; i < size; i++)
{
hash[arr[i]]++;
}
for ( auto i : hash)
{
if (i.second % 2 != 0)
{
return i.first;
}
}
return -1;
}
int main()
{
int arr[] = { 2, 3, 5, 4, 5, 2, 4,
3, 5, 2, 4, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getOddOccurrence(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.HashMap;
class OddOccurrence
{
static int getOddOccurrence( int arr[], int n)
{
HashMap<Integer,Integer> hmap = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (hmap.containsKey(arr[i]))
{
int val = hmap.get(arr[i]);
hmap.put(arr[i], val + 1 );
}
else
hmap.put(arr[i], 1 );
}
for (Integer a:hmap.keySet())
{
if (hmap.get(a) % 2 != 0 )
return a;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = new int []{ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 };
int n = arr.length;
System.out.println(getOddOccurrence(arr, n));
}
}
|
Python3
def getOddOccurrence(arr,size):
Hash = dict ()
for i in range (size):
Hash [arr[i]] = Hash .get(arr[i], 0 ) + 1 ;
for i in Hash :
if ( Hash [i] % 2 ! = 0 ):
return i
return - 1
arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ]
n = len (arr)
print (getOddOccurrence(arr, n))
|
C#
using System;
using System.Collections.Generic;
public class OddOccurrence
{
static int getOddOccurrence( int []arr, int n)
{
Dictionary< int , int > hmap = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (hmap.ContainsKey(arr[i]))
{
int val = hmap[arr[i]];
hmap.Remove(arr[i]);
hmap.Add(arr[i], val + 1);
}
else
hmap.Add(arr[i], 1);
}
foreach (KeyValuePair< int , int > entry in hmap)
{
if (entry.Value % 2 != 0)
{
return entry.Key;
}
}
return -1;
}
public static void Main(String[] args)
{
int []arr = new int []{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
int n = arr.Length;
Console.WriteLine(getOddOccurrence(arr, n));
}
}
|
Javascript
<script>
function getOddOccurrence(arr,n)
{
let hmap = new Map();
for (let i = 0; i < n; i++)
{
if (hmap.has(arr[i]))
{
let val = hmap.get(arr[i]);
hmap.set(arr[i], val + 1);
}
else
{
hmap.set(arr[i], 1);
}
}
for (let [key, value] of hmap.entries())
{
if (hmap.get(key) % 2 != 0)
return key;
}
return -1;
}
let arr=[2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2];
let n = arr.length;
document.write(getOddOccurrence(arr, n));
</script>
|
Output :
5
Time Complexity: O(n)
Auxiliary Space: O(n)
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++
#include <bits/stdc++.h>
using namespace std;
int getOddOccurrence( int ar[], int ar_size)
{
int res = 0;
for ( int i = 0; i < ar_size; i++)
res = res ^ ar[i];
return res;
}
int main()
{
int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
int n = sizeof (ar)/ sizeof (ar[0]);
cout << getOddOccurrence(ar, n);
return 0;
}
|
C
#include <stdio.h>
int getOddOccurrence( int ar[], int ar_size)
{
int res = 0;
for ( int i = 0; i < ar_size; i++)
res = res ^ ar[i];
return res;
}
int main()
{
int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "%d" , getOddOccurrence(ar, n));
return 0;
}
|
Java
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));
}
}
|
Python3
def getOddOccurrence(arr):
res = 0
for element in arr:
res = res ^ element
return res
arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ]
print ( "%d" % getOddOccurrence(arr))
|
C#
using System;
class GFG
{
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;
}
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));
}
}
|
Javascript
<script>
function getOddOccurrence( ar, ar_size)
{
let res = 0;
for (let i = 0; i < ar_size; i++)
res = res ^ ar[i];
return res;
}
let arr = [ 2, 3, 5, 4, 5, 2, 4,
3, 5, 2, 4, 4, 2 ];
let n = arr.length;
document.write(getOddOccurrence(arr, n));
</script>
|
PHP
<?php
function getOddOccurrence(& $ar , $ar_size )
{
$res = 0;
for ( $i = 0; $i < $ar_size ; $i ++)
$res = $res ^ $ar [ $i ];
return $res ;
}
$ar = array (2, 3, 5, 4, 5, 2,
4, 3, 5, 2, 4, 4, 2);
$n = sizeof( $ar );
echo (getOddOccurrence( $ar , $n ));
?>
|
Output :
5
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Oct, 2023
Like Article
Save Article