Skip to main content

350. Intersection of Two Arrays II

https://leetcode.com/problems/intersection-of-two-arrays-ii

Python

  • Time: O(n1 x log(n2))
  • Space: O(n1 & n2)
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
shorts, longs = (nums1, nums2) if len(nums1) < len(nums2) else (nums2, nums1)

result = []
for num in shorts:
if num in longs:
result.append(num)
longs[longs.index(num)] = None

return result