Number of unique pairs in an array

Given an array of N elements, the task is to find all the unique pairs that can be formed using the elements of a given array.

Examples:

Input: arr[] = {1, 1, 2}
Output: 4
(1, 1), (1, 2), (2, 1), (2, 2) are the only possible pairs.

Input: arr[] = {1, 2, 3}
Output: 9



Naive approach: The simple solution is to iterate through every possible pair and add them to a set and then find out the size of the set.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number
// of unique pairs in the array
int countUnique(int arr[], int n)
{
  
    // Set to store unique pairs
    set<pair<int, int> > s;
  
    // Make all possible pairs
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            s.insert(make_pair(arr[i], arr[j]));
  
    // Return the size of the set
    return s.size();
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 4, 2, 5, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << countUnique(arr, n);
    return 0;
}

chevron_right


Output:

25

Time Complexity: Time complexity of the above implementation is O(n2 Log n). We can optimize it to O(n2) using unordered_set with user defined hash function.

Efficient approach: First find out the number of unique elements in an array. Let the number of unique elements be x. Then, the number of unique pairs would be x2. This is because each unique element can form a pair with every other unique element including itself.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number
// of unique pairs in the array
int countUnique(int arr[], int n)
{
  
    unordered_set<int> s;
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
  
    int count = pow(s.size(), 2);
  
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 4, 2, 5, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << countUnique(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
    // Function to return the number
    // of unique pairs in the array
    static int countUnique(int arr[], int n) 
    {
  
        HashSet<Integer> s = new HashSet<>();
        for (int i = 0; i < n; i++)
        {
            s.add(arr[i]);
        }
        int count = (int) Math.pow(s.size(), 2);
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {1, 2, 2, 4, 2, 5, 3, 5};
        int n = arr.length;
        System.out.println(countUnique(arr, n));
    }
}
  
/* This code has been contributed 
by PrinciRaj1992*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the number 
# of unique pairs in the array 
def countUnique(arr, n) :
      
    s = set(); 
    for i in range(n) :
        s.add(arr[i]); 
  
    count = pow(len(s), 2); 
  
    return count; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 1, 2, 2, 4, 2, 5, 3, 5 ]; 
    n = len(arr);
  
    print(countUnique(arr, n));
      
# This code is contributed by Ryuga

chevron_right


Output:

25

Time Complexity: O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992, Ryuga