Another Method to Find Common Elements Using Set

To find the common elements in two arrays in C++, we have to create a set from the elements of the first array. Iterate through the second array and add elements that exist in the set to the result vector, returning it as the final output.

Approach

  • Initialize an unordered set to store elements from the first array.
  • Iterate through the first array and insert each element into the set.
  • Iterate through the second array:
    • For each element, check if it exists in the set:
    • If it does, add it to the result vector.
  • Return the result vector containing common elements.

C++ Program to Find the Common Elements in Two Arrays Using Set

The below example demonstrates how we can find the common elements in two arrays in C++

C++
// C++ Program to illustrate how to find the common elements
// in two arrays
#include <iostream>
#include <unordered_set>
#include <vector>

using namespace std;

// Function to find common elements between two vectors
vector<int> findCommonElements(const vector<int>& arr1,
                               const vector<int>& arr2)
{
    unordered_set<int> set;
    vector<int> result;

    // Insert all elements of first vector into set
    for (int num : arr1)
        set.insert(num);

    // Check elements of second vector
    for (int num : arr2) {
        // If element is present in set, it's common
        if (set.find(num) != set.end())
            result.push_back(num);
    }

    return result;
}

int main()
{
    vector<int> arr1 = { 1, 2, 3, 4, 5 };
    vector<int> arr2 = { 3, 4, 5, 6, 7 };

    vector<int> commonElements
        = findCommonElements(arr1, arr2);

    if (commonElements.empty()) {
        cout << "No common elements found." << endl;
    }
    else {
        cout << "Common elements: ";
        for (int elem : commonElements)
            cout << elem << " ";
        cout << endl;
    }

    return 0;
}

Output
Common elements: 3 4 5 

Time Complexity: O(N+ M), where N is the size of the first array and M is the size of the second array.
Auxilliary Space: O(N+M)





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 5

Similar Reads

Finding Common Elements Between Two Arrays in C++

To find the common elements in two arrays in C++, we have to first sort the arrays, then just iterate in the sorted arrays to find the common elements between those arrays....

Another Method to Find Common Elements Using Set

To find the common elements in two arrays in C++, we have to create a set from the elements of the first array. Iterate through the second array and add elements that exist in the set to the result vector, returning it as the final output....

Contact Us