intersection of two array code example
Example: intersection of two arrays
def intersection(nums1 , nums2):
result = []
memo = {}
for currentVal in nums1:
if currentVal in memo:
memo[currentVal] += 1
else:
memo[currentVal] = 1
for currentVal in nums2:
if currentVal in memo:
result.append(currentVal)
memo[currentVal] -= 1
if memo[currentVal] == 0:
del memo[currentVal]
return result
nums1 = [10, 10, 25, 14, 14, 14, 56]
nums2 = [10, 10, 14, 23, 34, 56]
print(intersection(nums1, nums2))