
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Number of Unique Pairs in an Array using C++
We require the appropriate knowledge to create several unique pairs in an array syntax in C++. In finding the number of unique pairs, we count all unique pairs in a given array, i.e., all possible pairs can be formed where each pair should be unique. For example −
Input : array[ ] = { 5, 5, 9 } Output : 4 Explanation : The number of all unique pairs are (5, 5), (5, 9), (9, 5) and (9, 9). Input : array[ ] = { 5, 4, 3, 2, 2 } Output : 16
Approach to find The Solution
There are Two Approaches for this Solution and they are −
Brute Force Approach
In this approach, we will traverse through each possible pair, add those pairs to a set, and finally find out the size of the set. The time complexity of this approach is O(n2 log n).
Example
#include <bits/stdc++.h> using namespace std; int main () { int arr[] = { 5, 4, 3, 2, 2 }; int n = sizeof (arr) / sizeof (arr[0]); // declaring set to store pairs. set < pair < int, int >>set_of_pairs; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) set_of_pairs.insert (make_pair (arr[i], arr[j])); int result = set_of_pairs.size(); cout <<"Number of unique pairs : " << result; return 0; }
Output
Number of unique pairs : 16
Explanation of the above code
In this code, first, we declare a set variable, and then, using two loops, we are traversing through each possible pair and inserting each pair in the set using i and j. Then we are calculating the size of the set and printing the result.
Efficient Approach
Another approach is to first find out the number of unique numbers in an array; now, every other unique element, including itself, may create a pair with any other unique element, so the number of unique pairs equals the square of the number of all unique numbers. The time complexity of his approach is O(n).
Example
#include <bits/stdc++.h> using namespace std; int main () { int arr[] = { 5, 4, 3, 2, 2 }; int n = sizeof (arr) / sizeof (arr[0]); // declaring set to store unique elements. unordered_set < int >set_of_elements; // inserting elements in the set. for (int i = 0; i < n; i++) set_of_elements.insert (arr[i]); int size = set_of_elements.size (); // finding number of unique pairs int result = size * size; cout << "Number of unique pairs in an array: " << result; return 0; }
Output
Number of unique pairs : 16
Explanation of the above code
In this code, we declare a set and then go through each element of the array inserting every element in the set. After that, we calculated the size of the set and found the result from formula n2, and printed the output.
Conclusion
In this article, we solve the problem of finding the number of unique pairs in an array where we discuss two ways to solve the problem, i.e., simple and efficient. In a simple approach, we insert all possible pairs in a set with time complexity of O(n2 log n), and in an efficient approach, we find all unique numbers and find the result with n2. We can write the same program in other languages such as C, java, python, and other languages. Hope you find this article helpful.
- Related Articles
- Find the Number of Prime Pairs in an Array using C++
- Find number of pairs in an array such that their XOR is 0 using C++.
- Find the Pairs of Positive Negative values in an Array using C++\n
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- Unique number of occurrences of elements in an array in JavaScript
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- Find the frequency of a number in an array using C++.
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Finding the only unique string in an array using JavaScript
- Unique pairs in array that forms palindrome words in JavaScript
- JavaScript Count the number of unique elements in an array of objects by an object property?
- C program to find the unique elements in an array.
- Program to find array of doubled pairs using Python
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
