Meta Data#
Difficulty: easy First Attempt: 2022-08-23
- Submissions: Accepted ×4
Approach#
Each value appears as many times as its smaller count. Counter’s & takes the smaller count per key (Hash Table). With sorted arrays, two pointers also work.
My Solution#
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list((Counter(nums1) & Counter(nums2)).elements())
Complexity#
Time O(m + n), space O(min(m, n)).
Mistakes to Avoid#
- Unlike LC 349, duplicates are kept here.
Interview Explanation#
Each value’s count is the smaller of its two counts, so I intersect Counters and expand the result. Time O(m + n).
