# Runtime: 40 ms, faster than 94.91% of Python3 online submissions for Intersection of Two Arrays II.
# Memory Usage: 14.2 MB, less than 5.72% of Python3 online submissions for Intersection of Two Arrays II.
from collections import Counter
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
sorted(nums1)
sorted(nums2)
if len(nums1) < len(nums2):
temp = nums1
comp = nums2
else:
temp = nums2
comp = nums1
num_count = Counter(temp)
ans = []
for i in range(len(comp)):
if comp[i] in num_count and num_count[comp[i]] > 0:
num_count[comp[i]] -= 1
ans.append(comp[i])
return ans
https://leetcode.com/problems/intersection-of-two-arrays-ii/