Find Unique pair in an array with pairs of numbers
Last Updated :
19 Jul, 2022
Given an array where every element appears twice except a pair (two elements). Find the elements of this unique pair.
Examples:
Input : 6, 1, 3, 5, 1, 3, 7, 6
Output : 5 7
All elements appear twice except 5 and 7
Input : 1 3 4 1
Output : 3 4
The idea is based on below post.
Find Two Missing Numbers | Set 2 (XOR based solution)
- XOR each element of the array and you will left with the XOR of two different elements which are going to be our result. Let this XOR be “XOR”
- Now find a set bit in XOR.
- Now divide array elements in two groups. One group that has the bit found in step 2 as set and other group that has the bit as 0.
- XOR of elements present in first group would be our first element. And XOR of elements present in second group would be our second element.
Implementation:
C++
#include <stdio.h>
void findUniquePair( int arr[], int n)
{
int XOR = arr[0];
for ( int i = 1; i < n; i++)
XOR = XOR ^ arr[i];
int set_bit_no = XOR & ~(XOR-1);
int x = 0, y = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
printf ( "The unique pair is (%d, %d)" , x, y);
}
int main()
{
int a[] = { 6, 1, 3, 5, 1, 3, 7, 6 };
int n = sizeof (a)/ sizeof (a[0]);
findUniquePair(a, n);
return 0;
}
|
Java
class GFG
{
static void findUniquePair( int [] arr, int n)
{
int XOR = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
XOR = XOR ^ arr[i];
int set_bit_no = XOR & ~(XOR- 1 );
int x = 0 , y = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((arr[i] & set_bit_no)> 0 )
x = x ^ arr[i];
else
y = y ^ arr[i];
}
System.out.println( "The unique pair is (" +
x + "," + y + ")" );
}
public static void main (String[] args) {
int [] a = { 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 };
int n = a.length;
findUniquePair(a, n);
}
}
|
Python 3
def findUniquePair(arr, n):
XOR = arr[ 0 ]
for i in range ( 1 , n):
XOR = XOR ^ arr[i]
set_bit_no = XOR & ~(XOR - 1 )
x = 0
y = 0
for i in range ( 0 , n):
if (arr[i] & set_bit_no):
x = x ^ arr[i]
else :
y = y ^ arr[i]
print ( "The unique pair is (" , x,
", " , y, ")" , sep = "")
a = [ 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 ]
n = len (a)
findUniquePair(a, n)
|
C#
using System;
class GFG {
static void findUniquePair( int [] arr, int n)
{
int XOR = arr[0];
for ( int i = 1; i < n; i++)
XOR = XOR ^ arr[i];
int set_bit_no = XOR & ~(XOR - 1);
int x = 0, y = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] & set_bit_no) > 0)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
Console.WriteLine( "The unique pair is ("
+ x + ", " + y + ")" );
}
public static void Main ()
{
int [] a = { 6, 1, 3, 5, 1, 3, 7, 6 };
int n = a.Length;
findUniquePair(a, n);
}
}
|
PHP
<?php
function findUniquePair( $arr , $n )
{
$XOR = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$XOR = $XOR ^ $arr [ $i ];
$set_bit_no = $XOR & ~( $XOR -1);
$x = 0;
$y = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] & $set_bit_no )
$x = $x ^ $arr [ $i ];
else
$y = $y ^ $arr [ $i ];
}
echo "The unique pair is " , "(" , $x , " " , $y , ")" ;
}
$a = array (6, 1, 3, 5, 1, 3, 7, 6);
$n = count ( $a );
findUniquePair( $a , $n );
?>
|
Javascript
<script>
function findUniquePair(arr, n)
{
let XOR = arr[0];
for (let i = 1; i < n; i++)
XOR = XOR ^ arr[i];
let set_bit_no = XOR & ~(XOR-1);
let x = 0, y = 0;
for (let i = 0; i < n; i++)
{
if ((arr[i] & set_bit_no)>0)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
document.write( "The unique pair is (" +
x + "," + y + ")" + "<br/>" );
}
let a = [ 6, 1, 3, 5, 1, 3, 7, 6 ];
let n = a.length;
findUniquePair(a, n);
</script>
|
Output
The unique pair is (7, 5)
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...