349. Intersection of Two Arrays
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the intersection of two integer arrays, nums1 and nums2. The key requirements are that the elements in the output array must be unique, and the order of the elements does not matter.
Solution
Intersection of Two Arrays in Python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
# Convert both lists to sets to get unique elements from each.
set1 = set(nums1)
set2 = set(nums2)
# The intersection of two sets can be found using the '&' operator.
# This operation is highly efficient and returns a new set containing
# elements that are common to both set1 and set2.
# This is an idiomatic way to find the intersection in Python.
intersection_set = set1 & set2
# Convert the resulting set of common elements back to a list and return it.
return list(intersection_set)
Intersection of Two Arrays in Java
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
// Create a HashSet to store unique elements of nums1 for O(1) average lookup time.
Set<Integer> set1 = new HashSet<>();
for (int num : nums1) {
set1.add(num);
}
// Create another HashSet to store the unique intersection results.
Set<Integer> resultSet = new HashSet<>();
// Iterate through the second array, nums2.
for (int num : nums2) {
// Check if the element from nums2 is present in set1.
if (set1.contains(num)) {
// If it exists, add it to the resultSet.
// The set will automatically handle uniqueness.
resultSet.add(num);
}
}
// Convert the resultSet to an integer array.
int[] result = new int[resultSet.size()];
int i = 0;
for (int num : resultSet) {
result[i++] = num;
}
return result;
}
}
Intersection of Two Arrays in C++
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
// Use an unordered_set to store unique elements of nums1 for O(1) average lookup time.
// The constructor efficiently populates the set from the vector's elements.
std::unordered_set<int> set1(nums1.begin(), nums1.end());
// Use another unordered_set to store the unique intersection results.
std::unordered_set<int> resultSet;
// Iterate through the second array, nums2.
for (int num : nums2) {
// Check if the element from nums2 is present in set1.
// .count() is an efficient way to check for existence in an unordered_set.
if (set1.count(num)) {
// If it exists, it's part of the intersection. Insert it into the resultSet.
// The set will automatically handle uniqueness.
resultSet.insert(num);
}
}
// Convert the resultSet to a vector and return it.
// The vector constructor can take two iterators to initialize its elements.
return std::vector<int>(resultSet.begin(), resultSet.end());
}
};
Intersection of Two Arrays in TypeScript
function intersection(nums1: number[], nums2: number[]): number[] {
// Create a Set from nums1 for O(1) average time lookups and to handle duplicates.
const set1 = new Set(nums1);
// Create a second Set to store the final unique intersection of elements.
const resultSet = new Set<number>();
// Iterate through the second array, nums2.
for (const num of nums2) {
// Check if the current number from nums2 exists in set1.
if (set1.has(num)) {
// If it exists, it's an intersection. Add it to our resultSet.
// The Set data structure will automatically handle uniqueness.
resultSet.add(num);
}
}
// Convert the resultSet back to an array.
// Array.from() creates a new Array instance from an iterable object like a Set.
return Array.from(resultSet);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.