Skip to content

Commit

Permalink
Intersection of Two Arrays II
Browse files Browse the repository at this point in the history
  • Loading branch information
24rochak committed Jul 20, 2019
1 parent 79603f8 commit a32d57b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Intersection of Two Arrays II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def intersect(nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""

a = {num: nums1.count(num) for num in nums1}
b = {num: nums2.count(num) for num in nums2}

c = []
for item in a:
c.extend([item] * min(a.get(item, 0), b.get(item, 0)))
return c


nums1 = [1, 2, 2, 1]
nums2 = [2, 2]
ans = intersect(nums1, nums2)
print(ans)

0 comments on commit a32d57b

Please sign in to comment.