📌  相关文章
📜  在具有成对数字的数组中查找唯一对

📅  最后修改于: 2021-04-23 18:07:36             🧑  作者: Mango

给定一个数组,其中每个元素(一对)(两个元素)出现两次。找到这对独特的元素。

例子:

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

这个想法是基于下面的帖子。

查找两个缺失的数字|第2组(基于XOR的解决方案)

1.对数组的每个元素进行XOR,您将剩下两个不同元素的XOR,这将成为我们的结果。将此XOR设为“ XOR
2.现在在XOR中找到一个设置位。
3.现在将数组元素分成两组。一组将在步骤2中找到的位设置为1,另一组将该位设置为0。
4.第一组中存在的元素的XOR将是我们的第一个元素。第二组中存在的元素的XOR将是我们的第二个元素。

C++
// C program to find a unique pair in an array
// of pairs.
#include 
  
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


输出:

The unique pair is (7, 5)