Given two integer arrays nums1
and nums2
, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4] Explanation: [4,9] is also accepted.
Constraints:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
This problem aims to find the intersection of two integer arrays, nums1
and nums2
. The intersection contains only unique elements that are present in both input arrays. The solution can be efficiently achieved using a hash table (or a set in some languages) to track elements from one array and then check for their presence in the other.
Create a Hash Table (or Set): We first create a hash table (or set) to store the unique elements from one of the input arrays (let's say nums1
). A hash table provides efficient O(1) average-case lookup time. Sets in many languages offer similar functionality.
Iterate and Check: We then iterate through the second array (nums2
). For each element in nums2
, we check if it exists in the hash table created in step 1.
Add to Result: If an element from nums2
is found in the hash table, it means it's present in both arrays. We add this element to the result array (ensuring uniqueness by using a set or checking before adding).
Return Result: Finally, we return the result array containing the unique intersection elements.
Time Complexity: O(m + n), where 'm' is the length of nums1
and 'n' is the length of nums2
. This is because we iterate through each array once. Hash table lookups are on average O(1).
Space Complexity: O(min(m, n)). In the worst case, the hash table will store all unique elements from the smaller of the two input arrays.
The following code examples demonstrate the solution using different programming languages. They all follow the same basic approach described above:
Python:
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set1 = set(nums1) # Create a set from nums1 for efficient lookups
result = set() # Use a set to ensure uniqueness in the result
for num in nums2:
if num in set1:
result.add(num)
return list(result) # Convert the set back to a list for output
Java:
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
for (int num : nums1) {
set1.add(num);
}
Set<Integer> result = new HashSet<>();
for (int num : nums2) {
if (set1.contains(num)) {
result.add(num);
}
}
int[] resultArray = new int[result.size()];
int i = 0;
for (int num : result) {
resultArray[i++] = num;
}
return resultArray;
}
}
JavaScript:
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function(nums1, nums2) {
const set1 = new Set(nums1);
const result = new Set();
for (const num of nums2) {
if (set1.has(num)) {
result.add(num);
}
}
return Array.from(result);
};
C++:
#include <vector>
#include <unordered_set>
class Solution {
public:
std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
std::unordered_set<int> set1(nums1.begin(), nums1.end());
std::unordered_set<int> result;
for (int num : nums2) {
if (set1.count(num)) {
result.insert(num);
}
}
return std::vector<int>(result.begin(), result.end());
}
};
These examples highlight the core logic. Adaptations might be needed for other languages, but the fundamental approach remains consistent. The use of sets/hash tables is crucial for achieving the optimal time complexity.