class Solution1(object):
def intersection(self, nums1, nums2):
"""
use set intersection
"""
return list(set(nums1).intersection(set(nums2)))
class Solution2(object):
def intersection(self, nums1, nums2):
"""
use dictionary, dictionary used to store num already appear to avoid redundancy
"""
dict1, dict2 = {}, {}
result = []
for num in nums1:
dict1[num] =1
for num in nums2:
if num in dict1 and num not in dict2:
result.append(num)
dict2[num] =1
return result
相关文章: