Skip to content

Commit 91a52c8

Browse files
committed
Intersection of Two Arrays
1 parent 6e87cab commit 91a52c8

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

349-intersection-of-two-arrays.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
Each element in the result must be unique.
1616
The result can be in any order.
1717
"""
18-
class Solution(object):
19-
def intersection(self, nums1, nums2):
20-
"""
21-
:type nums1: List[int]
22-
:type nums2: List[int]
23-
:rtype: List[int]
24-
"""
25-
d1,d2 = {}, {}
18+
class Solution:
19+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
20+
d = {}
21+
res = []
2622
for n in nums1:
27-
d1[n] = 0
23+
d[n] = 1
24+
2825
for n in nums2:
29-
if n in d1:
30-
d2[n] = 0
31-
return d2.keys()
26+
# Check if n is in dictionary and not in the result
27+
if n in d and d[n]:
28+
res.append(n)
29+
d[n] -= 1 # It will set the value of n = 0 which will indicate we already added n in result
30+
return res

0 commit comments

Comments
 (0)