VOOZH about

URL: https://www.geeksforgeeks.org/dsa/intersection-of-two-arrays/

⇱ Intersection of two Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Intersection of two Arrays

Last Updated : 27 Jul, 2025

Given two arrays a[] and b[], find their intersection — the unique elements that appear in both. Ignore duplicates, and the result can be in any order.

Input: a[] = [1, 2, 1, 3, 1], b[] = [3, 1, 3, 4, 1]
Output: [1, 3]
Explanation: 1 and 3 are the only common elements and we need to print only one occurrence of common elements

Input: a[] = [1, 1, 1], b[] = [1, 1, 1, 1, 1]
Output: [1]
Explanation: 1 is the only common element present in both the arrays.

Input: a[] = [1, 2, 3], b[] = [4, 5, 6]
Output: []
Explanation: No common element in both the arrays.

[Naive Approach] Using Triple Nested Loops - O(n × n × m) Time and O(1) Space

The idea is to find all unique elements that appear in both arrays by checking each element of one array against the other and ensuring no duplicates are added to the result.

Step By Step Implementation:

  • Initialize an empty array for result.
  • Traverse through a[] and for every element check if it is in b[], then check if it is already in result or not. If in b[] and not in result, then add it to the result.
  • Return result.

Output
2 3 

[Better Approach] Using Nested Loops and Hash Set - O(n × m) Time and O(n) Space

The idea is to find common elements between two arrays without duplicates by:|
– Using a hash set to track already added elements,
– Checking each element of a[] against b[], and only adding it to the result if it's present in b[] and not already recorded.

Step By Step Implementation:

  1. Initialize an empty hash set for storing result array elements
  2. Traverse through a[] and for every element check if it is in b[], then check if it is already in result or not. If in b[] and not in the hash set, then add it to the hash set.
  3. Create a result array and copy items of the hash set to the result array and return the result array.

Output
2 3 

[Expected Approach 1] Using Two Hash Sets - O(n+m) Time and O(n) Space

The idea is to use hash sets to efficiently find the unique elements that are common to both arrays. One set (as) stores elements from the first array, and the other (rs) ensures each common element is added only once to the result.

Step By Step Implementations:

  1. Initialize an empty array for result
  2. Create a hash set as (Set of a[] elements) and put all distinct items of a[] into it. For example, if array is [1, 2, 1, 3, 1], as is going to store [1, 2, 3]
  3. Create an empty hash set for result rs (Set of result elements)
  4. Create an empty array res[] to store result
  5. Traverse through all items of b[]. If an item is in as and not in rs, then add it to res[] and rs
  6. Return res[]

Output
3 2 

[Expected Approach 2] Using One Hash Set - O(n+m) Time and O(n) Space

We can optimize the above approach by avoiding creation of rs hash set. To make sure that duplicates are not added, we simply delete items from as (Set of a[] elements) rather than checking with rs.


Output
3 2 

Related Articles:

Comment