-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection of TwoArrays II.py
More file actions
37 lines (26 loc) · 1018 Bytes
/
Copy pathIntersection of TwoArrays II.py
File metadata and controls
37 lines (26 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
# res = []
# map1 = defaultdict(int)
# map2 = defaultdict(int)
# for i in range(len(nums1)):
# if nums1[i] not in map1:
# map1[nums1[i]] = 1
# else:
# map1[nums1[i]] += 1
# for i in range(len(nums2)):
# if nums2[i] not in map2:
# map2[nums2[i]] = 1
# else:
# map2[nums2[i]] += 1
# for n in map1:
# if n in map2:
# res.extend(min(map1[n], map2[n])*[n])
# return res
# Simplified:
map1 = collections.Counter(nums1)
map2 = collections.Counter(nums2)
res = []
for key in map1.keys() & map2.keys():
res.extend([key]*min(map1[key], map2[key]))
return res