Simple python solution


  • 0
    S
    def intersection(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: List[int]
            """
            inNums1 = {}
            answer  = []
            
            for x in nums1:
                inNums1[x] = 1
            
            for y in set(nums2):
                if inNums1.get(y) != None:
                    answer.append(y)
            
            return answer
    

    Pretty simple python solution. Let me know if I'm doing something inefficiently.


  • 2
    M
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        return list(set(nums1) & set(nums2))

  • 2
    H
    class Solution(object):
        def intersection(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: List[int]
            """
            return [x for x in list(set(nums1)) if x in nums2]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.