class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        ans=[]
        dict={}
        for i in range(len(nums2)):
            if nums2[i] in dict:
                dict[nums2[i]]+=1
            else:
                dict[nums2[i]]=1
        for i in range(len(nums1)):
            if nums1[i] in dict:
                if dict[nums1[i]]>0:
                    ans.append(nums1[i])
                    dict[nums1[i]]-=1
        return ans
s = Solution()
nums1 = [1,2,2,1]
nums2 = [2,2]
print(s.intersect(nums1,nums2))