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++
// C program to find a unique pair in an array
// of pairs.
#include <stdio.h>
void findUniquePair(int arr[], int n)
{
// XOR each element and get XOR of two unique
// elements(ans)
int XOR = arr[0];
for (int i = 1; i < n; i++)
XOR = XOR ^ arr[i];
// Now XOR has XOR of two missing elements. Any set
// bit in it must be set in one missing and unset in
// other missing number
// Get a set bit of XOR (We get the rightmost set bit)
int set_bit_no = XOR & ~(XOR-1);
// Now divide elements in two sets by comparing rightmost
// set bit of XOR with bit at same position in each element.
int x = 0, y = 0; // Initialize missing numbers
for (int i = 0; i < n; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i]; /*XOR of first set in arr[] */
else
y = y ^ arr[i]; /*XOR of second set in arr[] */
}
printf("The unique pair is (%d, %d)", x, y);
}
// Driver code
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
// Java program to find a unique pair
// in an array of pairs.
class GFG
{
static void findUniquePair(int[] arr, int n)
{
// XOR each element and get XOR of two
// unique elements(ans)
int XOR = arr[0];
for (int i = 1; i < n; i++)
XOR = XOR ^ arr[i];
// Now XOR has XOR of two missing elements.
// Any set bit in it must be set in one
// missing and unset in other missing number
// Get a set bit of XOR (We get the
// rightmost set bit)
int set_bit_no = XOR & ~(XOR-1);
// Now divide elements in two sets by
// comparing rightmost set bit of XOR with
// bit at same position in each element.
// Initialize missing numbers
int x = 0, y = 0;
for (int i = 0; i < n; i++)
{
if ((arr[i] & set_bit_no)>0)
/*XOR of first set in arr[] */
x = x ^ arr[i];
else
/*XOR of second set in arr[] */
y = y ^ arr[i];
}
System.out.println("The unique pair is (" +
x + "," + y + ")");
}
// Driver code
public static void main (String[] args) {
int[] a = { 6, 1, 3, 5, 1, 3, 7, 6 };
int n = a.length;
findUniquePair(a, n);
}
}
/* This code is contributed by Mr. Somesh Awasthi */
Python 3
# Python 3 program to find a unique
# pair in an array of pairs.
def findUniquePair(arr, n):
# XOR each element and get XOR
# of two unique elements(ans)
XOR = arr[0]
for i in range(1, n):
XOR = XOR ^ arr[i]
# Now XOR has XOR of two missing
# elements. Any set bit in it
# must be set in one missing and
# unset in other missing number
# Get a set bit of XOR (We get
# the rightmost set bit)
set_bit_no = XOR & ~(XOR - 1)
# Now divide elements in two sets
# by comparing rightmost set bit
# of XOR with bit at same position
# in each element.
x = 0
y = 0 # Initialize missing numbers
for i in range(0, n):
if (arr[i] & set_bit_no):
# XOR of first set in
# arr[]
x = x ^ arr[i]
else:
# XOR of second set
# in arr[]
y = y ^ arr[i]
print("The unique pair is (", x,
", ", y, ")", sep = "")
# Driver code
a = [6, 1, 3, 5, 1, 3, 7, 6 ]
n = len(a)
findUniquePair(a, n)
# This code is contributed by Smitha.
C#
// C# program to find a unique pair
// in an array of pairs.
using System;
class GFG {
static void findUniquePair(int[] arr, int n)
{
// XOR each element and get XOR of two
// unique elements(ans)
int XOR = arr[0];
for (int i = 1; i < n; i++)
XOR = XOR ^ arr[i];
// Now XOR has XOR of two missing
// elements. Any set bit in it must
// be set in one missing and unset
// in other missing number
// Get a set bit of XOR (We get the
// rightmost set bit)
int set_bit_no = XOR & ~(XOR - 1);
// Now divide elements in two sets by
// comparing rightmost set bit of XOR
// with bit at same position in each
// element. Initialize missing numbers
int x = 0, y = 0;
for (int i = 0; i < n; i++)
{
if ((arr[i] & set_bit_no) > 0)
/*XOR of first set in arr[] */
x = x ^ arr[i];
else
/*XOR of second set in arr[] */
y = y ^ arr[i];
}
Console.WriteLine("The unique pair is ("
+ x + ", " + y + ")");
}
// Driver code
public static void Main ()
{
int[] a = { 6, 1, 3, 5, 1, 3, 7, 6 };
int n = a.Length;
findUniquePair(a, n);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find a
// unique pair in an array
// of pairs.
function findUniquePair($arr, $n)
{
// XOR each element and
// get XOR of two unique
// elements(ans)
$XOR = $arr[0];
for ($i = 1; $i < $n; $i++)
$XOR = $XOR ^ $arr[$i];
// Now XOR has XOR of two
// missing elements. Any set
// bit in it must be set in
// one missing and unset in
// other missing number
// Get a set bit of XOR
// (We get the rightmost set bit)
$set_bit_no = $XOR & ~($XOR-1);
// Now divide elements in two
// sets by comparing rightmost
// set bit of XOR with bit at
// same position in each element.
// Initialize missing numbers
$x = 0;
$y = 0;
for ($i = 0; $i < $n; $i++)
{
if ($arr[$i] & $set_bit_no)
// XOR of first set in arr[]
$x = $x ^ $arr[$i];
else
// XOR of second set in arr[]
$y = $y ^ $arr[$i];
}
echo"The unique pair is ", "(",$x," ", $y,")";
}
// Driver code
$a = array(6, 1, 3, 5, 1, 3, 7, 6);
$n = count($a);
findUniquePair($a, $n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find a unique pair
// in an array of pairs.
function findUniquePair(arr, n)
{
// XOR each element and get XOR of two
// unique elements(ans)
let XOR = arr[0];
for (let i = 1; i < n; i++)
XOR = XOR ^ arr[i];
// Now XOR has XOR of two missing elements.
// Any set bit in it must be set in one
// missing and unset in other missing number
// Get a set bit of XOR (We get the
// rightmost set bit)
let set_bit_no = XOR & ~(XOR-1);
// Now divide elements in two sets by
// comparing rightmost set bit of XOR with
// bit at same position in each element.
// Initialize missing numbers
let x = 0, y = 0;
for (let i = 0; i < n; i++)
{
if ((arr[i] & set_bit_no)>0)
/*XOR of first set in arr[] */
x = x ^ arr[i];
else
/*XOR of second set in arr[] */
y = y ^ arr[i];
}
document.write("The unique pair is (" +
x + "," + y + ")" + "<br/>");
}
// driver function
let a = [ 6, 1, 3, 5, 1, 3, 7, 6 ];
let n = a.length;
findUniquePair(a, n);
</script>
OutputThe unique pair is (7, 5)
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Find pair with maximum GCD in an array We are given an array of positive integers. Find the pair in array with maximum GCD.Examples: Input : arr[] : { 1 2 3 4 5 }Output : 2Explanation : Pair {2, 4} has GCD 2 which is highest. Other pairs have a GCD of 1.Input : arr[] : { 2 3 4 8 8 11 12 }Output : 8Explanation : Pair {8, 8} has GCD 8 whic
15+ min read
Find index of pair among given pairs with just greater average Given an array of pairs arr[] of size N where the first value of all the pairs are distinct. For each pair of the given array find the index of another pair which have an average just greater than this one. Note: The average of two numbers a and b is defined as the floor ( a + b ) / 2. Examples: Inp
11 min read
Count number of pairs with the given Comparator Given an array arr[], the task is to count the number of pairs (arr[i], arr[j]) on the right of every element with any custom comparator. Comparator can be of any type, some of them are given below - arr[i] > arr[j], where i < j arr[i] < arr[j], where i 2 * arr[j], where i < j Examples:
15+ min read
Find the number of unique pairs satisfying given conditions Given an array arr[] of distinct positive elements, the task is to find the number of unique pairs (a, b) such that a is the maximum and b is the second maximum element of some subarray of the given array.Examples: Input: arr[] = {1, 2, 3} Output: 2 {1, 2}, {2, 3}, {1, 2, 3} are the subarrays and th
7 min read
Find all pairs with a given sum in two unsorted arrays Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to a given value X.Examples: Input: arr1[] = {-1, -2, 4, -6, 5, 7}, arr2[] = {6, 3, 4, 0} , x = 8Output: 4 4 5 3Input: arr1[] = {1, 2, 4, 5, 7}, arr2[] = {5, 6, 3, 4, 8}, x = 9Output: 1
13 min read