#include <bits/stdc++.h>
using namespace std;

// Function to find the intersection
// of two arrays
vector<int> Intersection(int arr1[], int arr2[], int n,
                         int m)
{
    set<int> st;

    // Removing duplicates from first array
    for (int i = 0; i < n; i++)
        st.insert(arr1[i]);

    vector<int> res;

    // Avoiding duplicates and adding intersections
    for (int i = 0; i < m; i++) {
        if (st.find(arr2[i]) != st.end()) {
            res.push_back(arr2[i]);
            st.erase(arr2[i]);
        }
    }
    return res;
}

// Driver code
int main()
{
    int arr1[] = { 1, 2, 4, 5, 6 };
    int arr2[] = { 2, 3, 5, 7 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int m = sizeof(arr2) / sizeof(arr2[0]);

    // Function call
    vector<int> inter = Intersection(arr1, arr2, n, m);
    for (int i : inter)
        cout << i << " ";
    return 0;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: