349. Intersection of Two Arrays
Easy
Problem:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.Solution:
Binary Search
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
result: Set = set()
nums2.sort()
for n1 in nums1:
i2 = bisect.bisect_left(nums2, n1)
if len(nums2) > 0 and len(nums2) > i2 and n1 == nums2[i2]:
result.add(n1)
return resultTwo Pointers
Last updated