350. Intersection of Two Arrays II

time: O(n+m)
space: O(min(n,m))
use hashmap in smaller array size one
follow up

if sorted, compare each other
in this way, you can reduce the memory usage (for huge data, this one dont need HashMap
Time Complexity: O(nlogn+mlogm), where nn and mm are the lengths of the arrays. We sort two arrays independently, and then do a linear scan.
Space Complexity: from O(logn+logm) to O(n+m), depending on the implementation of the sorting algorithm
Last updated
Was this helpful?