Find the element that appears once
Last Updated :
18 Apr, 2024
Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once.
Note: Expected time complexity is O(n) and O(1) extra space.
Examples:
Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}
Output: 2
In the given array all element appear three times except 2 which appears once.
Input: arr[] = {10, 20, 10, 30, 10, 30, 30}
Output: 20
In the given array all element appear three times except 20 which appears once.
Approach using Bitmask:
Use two variables, ones and twos, to track the bits that appear an odd and even number of times, respectively. In each iteration, XOR the current element with ones to update ones with the bits that appear an odd number of times then use a bitwise AND operation between ones and the current element to find the common bits that appear three times. These common bits are removed from both ones and twos using a bitwise AND operation with the negation of the common bits. Finally, ones contains the element that appears only once.
Below is the implementation of the above approach:
C++
// C++ program to find the element
// that occur only once
#include <bits/stdc++.h>
using namespace std;
int getSingle(int arr[], int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
// Let us take the example of
// {3, 3, 2, 3} to understand
// this
for (int i = 0; i < n; i++) {
/* The expression "one & arr[i]" gives the bits that
are there in both 'ones' and new element from arr[].
We add these bits to 'twos' using bitwise OR
Value of 'twos' will be set as 0, 3, 3 and 1 after
1st, 2nd, 3rd and 4th iterations respectively */
twos = twos | (ones & arr[i]);
/* XOR the new bits with previous 'ones' to get all
bits appearing odd number of times
Value of 'ones' will be set as 3, 0, 2 and 3 after
1st, 2nd, 3rd and 4th iterations respectively */
ones = ones ^ arr[i];
/* The common bits are those bits which appear third
time So these bits should not be there in both
'ones' and 'twos'. common_bit_mask contains all
these bits as 0, so that the bits can be removed
from 'ones' and 'twos'
Value of 'common_bit_mask' will be set as 00, 00, 01
and 10 after 1st, 2nd, 3rd and 4th iterations
respectively */
common_bit_mask = ~(ones & twos);
/* Remove common bits (the bits that appear third
time) from 'ones'
Value of 'ones' will be set as 3, 0, 0 and 2 after
1st, 2nd, 3rd and 4th iterations respectively */
ones &= common_bit_mask;
/* Remove common bits (the bits that appear third
time) from 'twos'
Value of 'twos' will be set as 0, 3, 1 and 0 after
1st, 2nd, 3rd and 4th iterations respectively */
twos &= common_bit_mask;
// uncomment this code to see intermediate values
// printf (" %d %d n", ones, twos);
}
return ones;
}
// Driver code
int main()
{
int arr[] = { 3, 3, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The element with single occurrence is "
<< getSingle(arr, n);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to find the element
// that occur only once
#include <stdio.h>
int getSingle(int arr[], int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
// Let us take the example of {3, 3, 2, 3} to understand this
for (int i = 0; i < n; i++) {
/* The expression "one & arr[i]" gives the bits that are
there in both 'ones' and new element from arr[]. We
add these bits to 'twos' using bitwise OR
Value of 'twos' will be set as 0, 3, 3 and 1 after 1st,
2nd, 3rd and 4th iterations respectively */
twos = twos | (ones & arr[i]);
/* XOR the new bits with previous 'ones' to get all bits
appearing odd number of times
Value of 'ones' will be set as 3, 0, 2 and 3 after 1st,
2nd, 3rd and 4th iterations respectively */
ones = ones ^ arr[i];
/* The common bits are those bits which appear third time
So these bits should not be there in both 'ones' and 'twos'.
common_bit_mask contains all these bits as 0, so that the bits can
be removed from 'ones' and 'twos'
Value of 'common_bit_mask' will be set as 00, 00, 01 and 10
after 1st, 2nd, 3rd and 4th iterations respectively */
common_bit_mask = ~(ones & twos);
/* Remove common bits (the bits that appear third time) from 'ones'
Value of 'ones' will be set as 3, 0, 0 and 2 after 1st,
2nd, 3rd and 4th iterations respectively */
ones &= common_bit_mask;
/* Remove common bits (the bits that appear third time) from 'twos'
Value of 'twos' will be set as 0, 3, 1 and 0 after 1st,
2nd, 3rd and 4th iterations respectively */
twos &= common_bit_mask;
// uncomment this code to see intermediate values
// printf (" %d %d n", ones, twos);
}
return ones;
}
int main()
{
int arr[] = { 3, 3, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The element with single occurrence is %d ",
getSingle(arr, n));
return 0;
}
Java
// Java code to find the element
// that occur only once
class GFG {
// Method to find the element that occur only once
static int getSingle(int arr[], int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
for (int i = 0; i < n; i++) {
/*"one & arr[i]" gives the bits that are there in
both 'ones' and new element from arr[]. We
add these bits to 'twos' using bitwise OR*/
twos = twos | (ones & arr[i]);
/*"one & arr[i]" gives the bits that are
there in both 'ones' and new element from arr[].
We add these bits to 'twos' using bitwise OR*/
ones = ones ^ arr[i];
/* The common bits are those bits which appear third time
So these bits should not be there in both 'ones' and 'twos'.
common_bit_mask contains all these bits as 0, so that the bits can
be removed from 'ones' and 'twos'*/
common_bit_mask = ~(ones & twos);
/*Remove common bits (the bits that appear third time) from 'ones'*/
ones &= common_bit_mask;
/*Remove common bits (the bits that appear third time) from 'twos'*/
twos &= common_bit_mask;
}
return ones;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 3, 3, 2, 3 };
int n = arr.length;
System.out.println("The element with single occurrence is " + getSingle(arr, n));
}
}
// Code contributed by Rishab Jain
Python3
# Python3 code to find the element that
# appears once
def getSingle(arr, n):
ones = 0
twos = 0
for i in range(n):
# one & arr[i]" gives the bits that
# are there in both 'ones' and new
# element from arr[]. We add these
# bits to 'twos' using bitwise XOR
twos = twos ^ (ones & arr[i])
# one & arr[i]" gives the bits that
# are there in both 'ones' and new
# element from arr[]. We add these
# bits to 'twos' using bitwise XOR
ones = ones ^ arr[i]
# The common bits are those bits
# which appear third time. So these
# bits should not be there in both
# 'ones' and 'twos'. common_bit_mask
# contains all these bits as 0, so
# that the bits can be removed from
# 'ones' and 'twos'
common_bit_mask = ~(ones & twos)
# Remove common bits (the bits that
# appear third time) from 'ones'
ones &= common_bit_mask
# Remove common bits (the bits that
# appear third time) from 'twos'
twos &= common_bit_mask
return ones
# driver code
arr = [3, 3, 2, 3]
n = len(arr)
print("The element with single occurrence is ",
getSingle(arr, n))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# code to find the element
// that occur only once
using System;
class GFG {
// Method to find the element
// that occur only once
static int getSingle(int[] arr, int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
for (int i = 0; i < n; i++) {
// "one & arr[i]" gives the bits
// that are there in both 'ones'
// and new element from arr[].
// We add these bits to 'twos'
// using bitwise OR
twos = twos | (ones & arr[i]);
// "one & arr[i]" gives the bits
// that are there in both 'ones'
// and new element from arr[].
// We add these bits to 'twos'
// using bitwise OR
ones = ones ^ arr[i];
// The common bits are those bits
// which appear third time So these
// bits should not be there in both
// 'ones' and 'twos'. common_bit_mask
// contains all these bits as 0,
// so that the bits can be removed
// from 'ones' and 'twos'
common_bit_mask = ~(ones & twos);
// Remove common bits (the bits that
// appear third time) from 'ones'
ones &= common_bit_mask;
// Remove common bits (the bits that
// appear third time) from 'twos'
twos &= common_bit_mask;
}
return ones;
}
// Driver code
public static void Main()
{
int[] arr = { 3, 3, 2, 3 };
int n = arr.Length;
Console.WriteLine("The element with single"
+ "occurrence is " + getSingle(arr, n));
}
}
// This Code is contributed by vt_m.
Javascript
<script>
// Javascript program for the above approach
// Method to find the element that occur only once
function getSingle(arr, n)
{
let ones = 0, twos = 0;
let common_bit_mask;
for (let i = 0; i < n; i++) {
/*"one & arr[i]" gives the bits that are there in
both 'ones' and new element from arr[]. We
add these bits to 'twos' using bitwise OR*/
twos = twos | (ones & arr[i]);
/*"one & arr[i]" gives the bits that are
there in both 'ones' and new element from arr[].
We add these bits to 'twos' using bitwise OR*/
ones = ones ^ arr[i];
/* The common bits are those bits which appear third time
So these bits should not be there in both 'ones' and 'twos'.
common_bit_mask contains all these bits as 0, so that the bits can
be removed from 'ones' and 'twos'*/
common_bit_mask = ~(ones & twos);
/*Remove common bits (the bits that appear third time) from 'ones'*/
ones &= common_bit_mask;
/*Remove common bits (the bits that appear third time) from 'twos'*/
twos &= common_bit_mask;
}
return ones;
}
// Driver Code
let arr = [ 3, 3, 2, 3 ];
let n = arr.length;
document.write("The element with single occurrence is " + getSingle(arr, n));
</script>
PHP
<?php
// PHP program to find the element
// that occur only once
function getSingle($arr, $n)
{
$ones = 0; $twos = 0 ;
$common_bit_mask;
// Let us take the example of
// {3, 3, 2, 3} to understand this
for($i = 0; $i < $n; $i++ )
{
/* The expression "one & arr[i]"
gives the bits that are there in
both 'ones' and new element from
arr[]. We add these bits to 'twos'
using bitwise OR
Value of 'twos' will be set as 0,
3, 3 and 1 after 1st, 2nd, 3rd
and 4th iterations respectively */
$twos = $twos | ($ones & $arr[$i]);
/* XOR the new bits with previous
'ones' to get all bits appearing
odd number of times
Value of 'ones' will be set as 3,
0, 2 and 3 after 1st, 2nd, 3rd and
4th iterations respectively */
$ones = $ones ^ $arr[$i];
/* The common bits are those bits
which appear third time. So these
bits should not be there in both
'ones' and 'twos'. common_bit_mask
contains all these bits as 0, so
that the bits can be removed from
'ones' and 'twos'
Value of 'common_bit_mask' will be
set as 00, 00, 01 and 10 after 1st,
2nd, 3rd and 4th iterations respectively */
$common_bit_mask = ~($ones & $twos);
/* Remove common bits (the bits
that appear third time) from 'ones'
Value of 'ones' will be set as 3,
0, 0 and 2 after 1st, 2nd, 3rd
and 4th iterations respectively */
$ones &= $common_bit_mask;
/* Remove common bits (the bits
that appear third time) from 'twos'
Value of 'twos' will be set as 0, 3,
1 and 0 after 1st, 2nd, 3rd and 4th
iterations respectively */
$twos &= $common_bit_mask;
// uncomment this code to see
// intermediate values
// printf (" %d %d n", ones, twos);
}
return $ones;
}
// Driver Code
$arr = array(3, 3, 2, 3);
$n = sizeof($arr);
echo "The element with single " .
"occurrence is ",
getSingle($arr, $n);
// This code is contributed by m_kit
?>
OutputThe element with single occurrence is 2
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach using Bit Manipulation:
The solution is based on the observation that in a binary representation of numbers, the bits that are set to 1 in the number that occurs only once will have a sum that is not a multiple of 3, while the bits that are set to 1 in the numbers that occur three times will have a sum that is a multiple of 3.
Here’s a breakdown of the intuition:
- Counting set bits: For each bit position (from least significant to most significant), iterate through the array and count the number of times the bit is set to 1 in each element. This gives us the sum of set bits for that particular position across all elements.
- Modulo 3: Take the modulo 3 of the sum of set bits for each position. If the result is not 0, it means that the number that occurs only once has a 1 in that bit position, while the numbers that occur three times have a 0 in that position.
- Reconstructing the number: Use the bits that have a sum that is not a multiple of 3 to reconstruct the binary representation of the number that occurs only once. You do this by setting the corresponding bits to 1 in the result variable.
Note: this approach won’t work for negative numbers
Below is the implementation of the above approach:
C++
// C++ program to find the element
// that occur only once
#include <bits/stdc++.h>
using namespace std;
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
// Initialize result
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver code
int main()
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The element with single occurrence is " << getSingle(arr, n);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to find the element
// that occur only once
#include <stdio.h>
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
// Initialize result
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver program to test above function
int main()
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The element with single occurrence is %d ",
getSingle(arr, n));
return 0;
}
Java
// Java code to find the element
// that occur only once
class GFG {
static final int INT_SIZE = 32;
// Method to find the element that occur only once
static int getSingle(int arr[], int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if ((arr[j] & x) != 0)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = arr.length;
System.out.println("The element with single occurrence is " + getSingle(arr, n));
}
}
// Code contributed by Rishab Jain
Python3
# Python3 code to find the element
# that occur only once
INT_SIZE = 32
def getSingle(arr, n) :
# Initialize result
result = 0
# Iterate through every bit
for i in range(0, INT_SIZE) :
# Find sum of set bits
# at ith position in all
# array elements
sm = 0
x = (1 << i)
for j in range(0, n) :
if (arr[j] & x) :
sm = sm + 1
# The bits with sum not
# multiple of 3, are the
# bits of element with
# single occurrence.
if ((sm % 3)!= 0) :
result = result | x
return result
# Driver program
arr = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
n = len(arr)
print("The element with single occurrence is ", getSingle(arr, n))
# This code is contributed
# by Nikita Tiwari.
C#
// C# code to find the element
// that occur only once
using System;
class GFG {
static int INT_SIZE = 32;
// Method to find the element
// that occur only once
static int getSingle(int[] arr, int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith
// position in all array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if ((arr[j] & x) != 0)
sum++;
}
// The bits with sum not multiple
// of 3, are the bits of element
// with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver Code
public static void Main()
{
int[] arr = { 12, 1, 12, 3, 12, 1,
1, 2, 3, 2, 2, 3, 7 };
int n = arr.Length;
Console.WriteLine("The element with single "
+ "occurrence is " + getSingle(arr, n));
}
}
// This code is contributed by vt_m.
Javascript
<script>
// Javascript program to find the element
// that occur only once
let INT_SIZE = 32;
function getSingle(arr, n)
{
// Initialize result
let result = 0;
let x, sum;
// Iterate through every bit
for (let i = 0; i < INT_SIZE; i++)
{
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (let j = 0; j < n; j++)
{
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver code
let arr = [ 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 ];
let n = arr.length;
document.write("The element with single occurrence is " + getSingle(arr, n));
// This code is contributed by mukesh07.
</script>
PHP
<?php
// PHP code to find the element
// that occur only once
$INT_SIZE= 32;
function getSingle($arr, $n)
{
global $INT_SIZE;
// Initialize result
$result = 0;
$x; $sum;
// Iterate through every bit
for ($i = 0; $i < $INT_SIZE; $i++)
{
// Find sum of set bits at ith
// position in all array elements
$sum = 0;
$x = (1 << $i);
for ($j = 0; $j < $n; $j++ )
{
if ($arr[$j] & $x)
$sum++;
}
// The bits with sum not multiple
// of 3, are the bits of element
// with single occurrence.
if (($sum % 3) !=0 )
$result |= $x;
}
return $result;
}
// Driver Code
$arr = array (12, 1, 12, 3, 12, 1,
1, 2, 3, 2, 2, 3, 7);
$n = sizeof($arr);
echo "The element with single occurrence is ",
getSingle($arr, $n);
// This code is contributed by ajit
?>
OutputThe element with single occurrence is 7
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...