How to Find Common Elements Between Two Arrays using STL in C++?
Last Updated :
08 Oct, 2024
Given two arrays, the task is to find all the elements that are present in both the arrays i.e. common elements between both arrays in C++.
Example:
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.
There are two primary ways in STL using which we can find the common elements in two arrays:
Using std::set_intersection()
C++ STL provides the std::set_intersection() function that is used to find the common elements (also called intersection) of the two given ranges. But the prerequisite for this function is that both the ranges should be sorted.
Syntax of std::set_intersection()
std::set_intersection(first1, last1, first2, last2, dest);
where first1, first2 and last1, last2 are the ranges and dest is the iterator to the starting of the container where the result is stored.
Code Implementation
CPP
// C++ program to find common elements between
// two Arrays using set_intersection()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr1[] = {1, 45, 54, 71, 76, 12};
int arr2[] = {1, 7, 5, 4, 6, 12};
// Compute the sizes
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
// Sort the arrays
sort(arr1, arr1 + n1);
sort(arr2, arr2 + n2);
// Vector to store result
vector<int> v;
// Finding common elements using
// set_intersection() function
set_intersection(arr1, arr1 + n1, arr2,
arr2 + n2, back_inserter(v));
for (auto i: v){
cout << i << " ";
}
return 0;
}
Time Complexity: O(n*logn + m*logm), where n and m are the sizes of first and second array.
Auxiliary Space: O(1) as no extra space is used by algorithm itself.
Using std::unordered_set Container
We can find the common elements between two arrays by inserting elements of one array into an std::unordered_set container and then checking if element from the second array exist in the set or not. If it exists, it is common, otherwise, it is unique.
We can also use other containers but none of them are as efficient as std::unordered_set. We can also take the smaller array for std::unordered_set mapping to optimize space.
Code Implementation
C++
// C++ program to find common elements between
// two arrays using unordered_set
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr1[] = {1, 45, 54, 71, 76, 12};
int arr2[] = {1, 7, 5, 4, 6, 12};
// Compute the sizes
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
// Create an unordered_set from the first array
unordered_set<int> s(arr1, arr1 + n1);
// Vector to store the common elements
vector<int> v;
// Finding common elements by checking
// if elements of arr2 exist in the set
for (int i = 0; i < n2; i++) {
if (s.find(arr2[i]) != s.end()) {
v.push_back(arr2[i]);
}
}
for (auto i: v) cout << i << " ";
return 0;
}
Time Complexity: O(n + m), where n and m are the sizes of first and second array.
Auxiliary Space: O(n), where n is the size of smaller array.
Apart from the methods provided in STL, there are also many different methods to find the common elements between two arrays in C++.
Similar Reads
How to Find Common Elements in Two Arrays in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the common elements in two arrays in C++. Examples: Input:Arr1: {1, 2, 3, 4, 5}Arr2: {3, 4, 5, 6, 7}Output:Common Elements: 3 4
4 min read
How to Find the Unique Elements in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2
2 min read
How to Concatenate Two Arrays in C++? In C++, arrays store a fixed number of elements of the same type in contiguous memory locations. In this article, we will learn how to concatenate two arrays in C++. Example: Input: myArray1={10, 30, 40} myArray2 ={20, 50, 60}Output: Concatenated Array: 10 30 40 20 50 60 Concatenate Two Arrays in On
2 min read
C++ Program to Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9} Output: 1 1 is missing from second array. Input: arr1[] = {2, 3, 4, 5} arr
4 min read
How to Find the Union of Two Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how to find the union of two sets in C++. Example Input:set1 = {12 , 13, 14
2 min read
Maximum number of uncrossed lines between two given arrays Given two arrays A[] and B[], the task is to find the maximum number of uncrossed lines between the elements of the two given arrays. A straight line can be drawn between two array elements A[i] and B[j] only if: A[i] = B[j]The line does not intersect any other line.Examples: Input: A[] = {3, 9, 2},
15+ min read
How to Find the Difference of Two Sets in C++? In C++, the set container provides an efficient way to store unique elements in sorted order. Finding the difference between two sets involves determining the elements that are present in one set but not in the other. In this article, we are going to learn how to find the difference of two sets in C
2 min read
How to Find Union of Two Multisets in C++? In C++, multisets are a type of associative container similar to the set, with the exception that multiple elements can have the same values. Union is In this article, we will see how to find the union of two multisets in C++ STL. Example Input: first_multiset = {100,300,400,500,200,500,200}, second
3 min read
How to Concatenate Two Sets in C++? In C++, sets are the data containers that store the unique elements in some specified order. Concatenating two sets means merging the elements of two sets into the first set. In this article, we will learn how to concatenate two sets in C++ STL. Example Input: mySet1 = {1, 2, 3, 4, 5, 8, 9} mySet2 =
2 min read
How to Compare Arrays in C++? In C++, arrays are linear data structures that can store data of the same type in contiguous memory locations. In this article, we will learn how to compare two arrays to check whether they are equal or not in C++. Example: Input: arr1[] = {1, 2, 4, 3, 5, 11} arr2[] = {1 2, 3, 4 ,5} Output: arr1 and
2 min read