Find Unique pair in an array with pairs of numbers Last Updated : 19 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice 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 4Recommended PracticeFind Unique pair in an array with pairs of numbersTry It! 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) Comment More infoAdvertise with us Next Article Find Unique pair in an array with pairs of numbers D Dhiman Mayank Improve Article Tags : Misc Bit Magic DSA Arrays Bitwise-XOR +1 More Practice Tags : ArraysBit MagicMisc Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Like