Skip to content

Instantly share code, notes, and snippets.

@varnie
Created July 27, 2025 14:59
Show Gist options
  • Select an option

  • Save varnie/53cbf1f73ec52ee5257618535ec279df to your computer and use it in GitHub Desktop.

Select an option

Save varnie/53cbf1f73ec52ee5257618535ec279df to your computer and use it in GitHub Desktop.
```
Intersection of two arrays II
Given two integer arrays nums1 and nums2, return the array of their intersection.
Each element in the result must appear as many times as it appears in both arrays.
You can return the result in any order.
Example 1:
Input:
nums1 = [1,2,2,1], nums2 = [2,2]
Output:
[2,2]
Example 2:
Input:
nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output:
[4,9]
Explanation: [9,4] is also the correct answer.
Limitations:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
```
class Solution:
def intersect(self, nums1: list[int], nums2: list[int]) -> list[int]:
result = []
for elem in nums1:
if elem in nums2:
result.append(elem)
nums2.remove(elem)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment