Intersection of Two Arrays

Problem: Intersection of Two Arrays

We can use a hash table to store the occurence of elements in the first array and check whether the elements in the second array is in the hash table.

Code in Python:

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        m = {}
        for num in nums1:
            m[num] = 1
        res = []
        for num in nums2:
            if num in m:
                res.append(num)
                del m[num]
        return res

results matching ""

    No results matching ""