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.
Note: The intersection of two arrays is defined as the set of elements that are present in both arrays.
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 <= 10000 <= nums1[i], nums2[i] <= 1000The simplest approach is to check every element in the first array against every element in the second array. When we find a match, we add it to our result set. Using a set ensures we only include each common element once, even if it appears multiple times in both arrays.
res to store unique intersection elements.i in nums1:j in nums2:i == j, add i to res and break the inner loop.res to a list and return it.Where is the size of the array and is the size of the array .
By sorting both arrays, we can use two pointers to efficiently find common elements. We advance the pointer pointing to the smaller element. When both pointers point to equal elements, we found an intersection. We skip duplicates to ensure each element appears only once in the result.
nums1 and nums2.i and j at the start of each array.j while nums2[j] < nums1[i].nums1[i] == nums2[j], add to result.i, skipping any duplicates.class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1.sort()
nums2.sort()
n, m = len(nums1), len(nums2)
res, i, j = [], 0, 0
while i < n and j < m:
while j < m and nums2[j] < nums1[i]:
j += 1
if j < m:
if nums1[i] == nums2[j]:
res.append(nums1[i])
i += 1
while i < n and nums1[i] == nums1[i - 1]:
i += 1
return resWhere is the size of the array and is the size of the array .
Converting both arrays to sets removes duplicates and enables O(1) lookup. We then iterate through one set and check membership in the other. Any element present in both sets belongs to the intersection.
nums1 to a set set1.nums2 to a set set2.set1:set2, add it to the result.Where is the size of the array and is the size of the array .
We use a hash map to track which elements from nums1 we have seen. We mark each element with value 1. When iterating through nums2, if we find a marked element, we add it to the result and set its value to 0 to prevent duplicates.
seen and mark all elements in nums1 with value 1.nums2:seen[num] == 1, add num to result and set seen[num] = 0.Where is the size of the array and is the size of the array .
We can improve on the two-set approach by using only one set. Store all elements from nums1 in a set, then iterate through nums2. When we find a match, add it to the result and remove it from the set to avoid duplicates.
seen from all elements in nums1.nums2:seen, add it to the result and remove it from seen.Where is the size of the array and is the size of the array .
Most programming languages provide built-in set intersection operations. By converting both arrays to sets and using the intersection function, we get a clean one-liner solution. The implementation details are handled by the language's standard library.
nums1 to a set.nums2 to a set.Where is the size of the array and is the size of the array .
The intersection should contain each common element only once, even if it appears multiple times in both arrays. For example, if nums1 = [1, 1, 2] and nums2 = [1, 1], the result should be [1], not [1, 1]. Using a set for the result or marking elements as "already added" prevents this issue.
A brute force approach comparing every pair of elements is O(n * m), which is inefficient for large arrays. The optimal approach uses a hash set for O(1) lookups, reducing time complexity to O(n + m). Always consider converting one array to a set before checking membership.