Skip to content

Commit cd7fd43

Browse files
solves intersection of array 2 in python
1 parent 89c5051 commit cd7fd43

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
| 345 | [Reverse Vowels of A String](https://leetcode.com/problems/reverse-vowels-of-a-string) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ReverseVowelsOfString.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/reverse_vowels_of_a_string.py) |
9999
| 346 | 🔒 [Moving Average From Data Stream](https://leetcode.com/problems/moving-average-from-data-stream) | Easy | |
100100
| 349 | [Intersection of 2 Arrays](https://leetcode.com/problems/intersection-of-two-arrays) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArrays.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/intersection_of_2_array.py) |
101-
| 350 | [Intersection of 2 Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArraysII.java) |
101+
| 350 | [Intersection of 2 Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArraysII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/intersection_of_2_arrays_II.py) |
102102
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter) | Easy | |
103103
| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IsPerfectSquare.java) |
104104
| 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/GuessNumberHigherOrLower.java) |

python/intersection_of_2_arrays_II.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List, Dict
2+
3+
4+
class Solution:
5+
def get_frequency(self, numbers: List[int]) -> Dict[int, int]:
6+
frequency = {}
7+
for number in numbers:
8+
frequency[number] = frequency.get(number, 0) + 1
9+
return frequency
10+
11+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
12+
result = []
13+
nums1, nums2 = (nums1, nums2) if len(nums1) < len(nums2) else (nums2, nums1)
14+
nums_1_freq = self.get_frequency(nums1)
15+
for number in nums2:
16+
if nums_1_freq.get(number, 0) >= 1:
17+
result.append(number)
18+
nums_1_freq[number] -= 1
19+
return result

0 commit comments

Comments
 (0)