Problem can be found in here!
Solution: Hash Table
def intersect(nums1: List[int], nums2: List[int]) -> List[int]:
memo = defaultdict(int)
for num in nums1:
memo[num] += 1
output_list = []
for num in nums2:
if memo[num] > 0:
output_list.append(num)
memo[num] -= 1
return output_list
Explanation: Instead of using two sets to solve this problem as Intersection of Two Arrays, we can use a hash table or dictionary in Python to store the maximum number of occurrences for each character. Therefore, the returned value will appear as many times as it shows in both arrays.
Time Complexity: , Space Complexity: , where n and m is the length of nums1 and nums2, respectively.