Created
July 27, 2025 14:59
-
-
Save varnie/53cbf1f73ec52ee5257618535ec279df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` | |
| 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