Answers for "intersection of two arrays hashmap"

0

intersection of two arrays hashmap

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))
Posted by: Guest on June-20-2021

Code answers related to "intersection of two arrays hashmap"

Python Answers by Framework

Browse Popular Code Answers by Language