When working with arrays, one common problem in Data Structures and Algorithms (DSA) is to find the intersection โ€” i.e., elements that appear in both arrays.

For example, if you have two lists of numbers, you might want to find out which numbers are common to both. This is a common question in coding interviews, and it helps you strengthen your logic-building skills.

๐Ÿ’ก What Is the Intersection of Two Arrays?

The intersection of two arrays means the set of elements that appear in both arrays.

Example

Code
Array A = [1, 2, 3, 4, 5]
Array B = [3, 4, 5, 6, 7]

โœ… Intersection = [3, 4, 5]

These numbers are present in both arrays A and B.

โš™๏ธ Approach 1. Brute Force (Simple but Less Efficient)

๐Ÿง  Logic

  • For each element in array A, check whether it exists in array B.

  • If yes, add it to the intersection list.

โฑ๏ธ Time Complexity: O(nยฒ)

๐Ÿ’พ Space Complexity: O(1)

๐Ÿ’ป C++ Code Example

Code
#include <iostream>using namespace std;

int main() {
    int A[] = {1, 2, 3, 4, 5};
    int B[] = {3, 4, 5, 6, 7};
    int n = 5, m = 5;

    cout << "Intersection: ";
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(A[i] == B[j]) {
                cout << A[i] << " ";
                break;
            }
        }
    }
    return 0;
}

โœ… Output

Code
Intersection: 3 4 5

โšก Approach 2. Using Hashing (Efficient and Common)

๐Ÿง  Logic

  • Store all elements of the first array in a hash set.

  • Traverse the second array, and check if an element exists in the set.

  • If yes, add it to the intersection.

โฑ๏ธ Time Complexity: O(n + m)

๐Ÿ’พ Space Complexity: O(n)

๐Ÿ’ป C++ Code Example

Code
#include <iostream>#include <unordered_set>using namespace std;

int main() {
    int A[] = {1, 2, 3, 4, 5};
    int B[] = {3, 4, 5, 6, 7};
    int n = 5, m = 5;

    unordered_set<int> s(A, A + n);
    cout << "Intersection: ";

    for (int i = 0; i < m; i++) {
        if (s.find(B[i]) != s.end()) {
            cout << B[i] << " ";
            s.erase(B[i]); // to avoid duplicates
        }
    }
    return 0;
}

โœ… Output

Code
Intersection: 3 4 5

๐ŸŒŸ Why Use Hashing?

Hashing provides O(1) average lookup time, making it much faster than the brute-force method, especially for large datasets.

๐Ÿš€ Approach 3. Two-Pointer Technique (When Arrays Are Sorted)

๐Ÿง  Logic

  1. Sort both arrays if not sorted.

  2. Use two pointers โ€” one for each array.

  3. Compare elements:

    • If A[i] == B[j], add to intersection and move both pointers.

    • If A[i] < B[j], move i forward.

    • Else, move j forward.

โฑ๏ธ Time Complexity: O(n + m)

๐Ÿ’พ Space Complexity: O(1)

๐Ÿ’ป C++ Code Example

Code
#include <iostream>#include <algorithm>using namespace std;

int main() {
    int A[] = {1, 2, 3, 4, 5};
    int B[] = {3, 4, 5, 6, 7};
    int n = 5, m = 5;

    int i = 0, j = 0;

    cout << "Intersection: ";
    while (i < n && j < m) {
        if (A[i] == B[j]) {
            cout << A[i] << " ";
            i++;
            j++;
        }
        else if (A[i] < B[j])
            i++;
        else
            j++;
    }
    return 0;
}

โœ… Output

Code
Intersection: 3 4 5

๐Ÿ“Š Comparison Table

ApproachTime ComplexitySpace ComplexitySuitable For
Brute ForceO(nยฒ)O(1)Small arrays
HashingO(n + m)O(n)Unsorted arrays
Two PointerO(n + m)O(1)Sorted arrays

๐Ÿง  Real-Life Use Case

  • Finding common friends between two users on social media.

  • Identifying common customers between two databases.

  • Comparing tags or keywords shared between two documents.

๐Ÿ Conclusion

Finding the intersection of two arrays is a fundamental DSA problem that strengthens your understanding of searching, hashing, and sorting.

  • Start with the brute force approach for clarity.

  • Then move to hashing or two-pointer for efficiency.

Mastering these methods will help you in technical interviews, especially for roles involving data structures and algorithms.