Given two arrays, the task is to find the elements that are present in both arrays.
- An element is considered common if it occurs in both arrays.
- The approaches below find the intersection of the two arrays using STL containers and algorithms.
Note: The examples below assume that each common element is reported once. If duplicate frequencies need to be preserved, the set_intersection() approach follows multiset-style frequency matching, while the unordered_set approach should explicitly avoid duplicate output.
Examples:
Input: arr1[] = {1, 45, 54, 71, 76, 12}
arr2[] = {1, 7, 5, 4, 6, 12}
Output: 1 12
Explanation: The common elements between the two arrays are 1 and 12.Input: arr1[] = {1, 7, 5, 4, 6, 12}
arr2[] = {10, 12, 11}
Output: 12
Explanation: The only common element between the two arrays is 12.
Approaches to Find Common Elements
The common elements can be found using the following STL approaches:
Using std::set_intersection()
The std::set_intersection() algorithm finds the intersection of two sorted ranges. Therefore, both arrays must be sorted before applying it.
Syntax
std::set_intersection(first1, last1,
first2, last2,
destination);
Here:
- first1 and last1 define the first sorted range.
- first2 and last2 define the second sorted range.
- destination specifies where the intersection is stored.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr1[] = {1, 45, 54, 71, 76, 12};
int arr2[] = {1, 7, 5, 4, 6, 12};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
// Sort both arrays
sort(arr1, arr1 + n1);
sort(arr2, arr2 + n2);
vector<int> common;
// Find common elements
set_intersection(arr1, arr1 + n1,
arr2, arr2 + n2,
back_inserter(common));
for (int x : common)
cout << x << " ";
return 0;
}
Output
1 12
Explanation: The arrays are first sorted so that set_intersection() can compare their elements efficiently. The algorithm then traverses both ranges and stores the elements that occur in both arrays in common.
Using std::unordered_set
Another approach is to store the elements of one array in an unordered_set and check whether each element of the other array is present in the set. This avoids sorting and provides average O(1) lookup time.
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
int main()
{
int arr1[] = {1, 45, 54, 71, 76, 12};
int arr2[] = {1, 7, 5, 4, 6, 12};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
unordered_set<int> elements(arr1, arr1 + n1);
unordered_set<int> common;
// Check elements of arr2
for (int i = 0; i < n2; i++)
{
if (elements.find(arr2[i]) != elements.end())
common.insert(arr2[i]);
}
for (int x : common)
cout << x << " ";
return 0;
}
Output
12 1
Explanation
- The elements of arr1 are stored in an unordered_set. Each element of arr2 is then checked against the set. If it is found, it is inserted into common.
- Using another unordered_set for the result ensures that a common element is printed only once, even if it occurs multiple times in arr2.