350 - Intersection Of Two Arrays II
Written on November 4, 2017
Tweet
Each element in the result should appear as many times as it shows in both arrays. The result can be in any order.
class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
c1, c2 = Counter(nums1), Counter(nums2)
ret = []
for num in c1 & c2:
ret.extend([num] * min(c1[num], c2[num]))
return ret