Intersection of Two Arrays II

Problem: Intersection of Two Arrays II

The only difference from the first problem is that we need to take care of times of occurence of elements.

Code in Python:

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        m = {}
        for num in nums1:
            m[num] = m[num] + 1 if num in m else 1
        res = []
        for num in nums2:
            if num in m and m[num] > 0:
                res.append(num)
                m[num] -= 1
        return res

results matching ""

    No results matching ""