Scribbling

LeetCode: 350. Intersection of Two Arrays II 본문

Computer Science/Coding Test

LeetCode: 350. Intersection of Two Arrays II

focalpoint 2022. 1. 10. 22:04

Using double counters.

class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        from collections import Counter
        c1 = Counter(nums1)
        c2 = Counter(nums2)
        ret = []
        for k in c2.keys():
            ret.extend([k] * min(c1[k], c2[k]))
        return ret