Skip to content

Commit f2fb5fc

Browse files
Add files via upload
1 parent fdb565d commit f2fb5fc

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
counter1 = Counter(nums1)
4+
counter2 = Counter(nums2)
5+
6+
# Using defaultdict to handle missing keys more efficiently
7+
counter1 = defaultdict(int, counter1)
8+
counter2 = defaultdict(int, counter2)
9+
10+
intersection = []
11+
12+
for num, freq in counter1.items():
13+
min_freq = min(freq, counter2[num])
14+
if min_freq > 0:
15+
intersection.extend([num] * min_freq)
16+
17+
return intersection

0 commit comments

Comments
 (0)