What is the intersection of 2 arrays
Sigiloso
Intersection of Two Arrays – Summary 👉 Clarify first: Should intersection include duplicates or only unique values? 1. Unique Elements Only Use a set for fast lookup. Algo: set1 = set(arr1) Loop through arr2, check if in set1 Store matches in result set. Time: O(n + m), Space: O(n) 2. Include Duplicates Use hashmap (Counter) to track frequencies. Algo: Count elements in arr1 → Counter(arr1) Loop arr2, if in map and count > 0: Add to result, decrement count. Time: O(n + m), Space: O(n) ✅ Use set when you only care about presence. ✅ Use Counter/dict when duplicates matter.