Empresa engajada
Write algorithm to compute the intersection of two arrays. What is the time complexity of this algorithm (using the big O notation)?
Sigiloso
You supposed to use hashmaps. And its time complexity is O(n+m). n = length of 1st array and m = length of 2nd array. Here is algoritm in python def array_intersect(arr1, arr2): cache = dict() result = [] for elm in arr1: if elm in cache: cache[elm] +=1 else: cache[elm] = 1 for elm in arr2: if elm in cache: result.append(elm) cache[elm] -=1 return result