Open In App

How to Find Common Elements Between Two Arrays using STL in C++?

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
1 12 

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;
}

Output
1 12 

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++.



Next Article
Practice Tags :

Similar Reads