πDay 4. Intersection of Two arrays with Duplicate Elements π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
You are given two integer arrays a[]
and b[]
. Your task is to find the intersection of the two arrays, which includes the elements that are common in both arrays. The intersection should not contain duplicate elements, and the result can be in any order.
π Example Walkthrough:
Input:
a[] = [1, 2, 1, 3, 1], b[] = [3, 1, 3, 4, 1]
Output:
[1, 3]
Explanation:
1 and 3 are the only common elements, and only one occurrence of each is included in the result.
Input:
a[] = [1, 1, 1], b[] = [1, 1, 1, 1, 1]
Output:
[1]
Explanation:
1 is the only common element present in both arrays.
Input:
a[] = [1, 2, 3], b[] = [4, 5, 6]
Output:
[]
Explanation:
No common elements exist in both arrays.
Constraints
$( 1 \leq \text{size of } a, b \leq 10^5 )$
$( 1 \leq a[i], b[i] \leq 10^5 )$
π― My Approach:
Optimized Intersection with Hashing
Use of Hash Set:
We utilize a hash set to store the elements of the first array
a[]
. This allows for efficient lookups to check if an element from the second arrayb[]
is present ina[]
.
Steps to Solve the Problem:
Insert all elements of
a[]
into a hash setsetA
.Iterate through
b[]
. For each element inb[]
, check if it exists insetA
:If yes, add the element to the result and remove it from
setA
to ensure duplicates are avoided.
Return the resulting list of common elements.
Why Efficient?
Hash sets allow O(1) average time complexity for both insertion and lookup.
This approach minimizes unnecessary comparisons, making it suitable for large arrays.
π Time and Auxiliary Space Complexity
Expected Time Complexity:
$( O(n + m) )$, where $( n )$ is the size of array
a
and $( m )$ is the size of arrayb
.Constructing the hash set takes $( O(n) )$, and traversing array
b
takes $( O(m) )$.
Expected Auxiliary Space Complexity:
$( O(\min(n, m)) )$, as the hash set stores at most the unique elements from the smaller array.
π Solution Code
Code (C++)
class Solution {
public:
vector<int> intersectionWithDuplicates(vector<int>& a, vector<int>& b) {
unordered_set<int> sa(a.begin(), a.end());
unordered_set<int> res_set;
for (int num : b) {
if (sa.erase(num)) {
res_set.insert(num);
}
}
return vector<int>(res_set.begin(), res_set.end());
}
};
Code (Java)
class Solution {
public ArrayList<Integer> intersectionWithDuplicates(int[] a, int[] b) {
HashSet<Integer> setA = new HashSet<>();
ArrayList<Integer> result = new ArrayList<>();
for (int num : a) setA.add(num);
for (int num : b) {
if (setA.remove(num)) result.add(num);
}
return result;
}
}
Code (Python)
class Solution:
def intersectionWithDuplicates(self, a, b):
set_a = set(a)
result = []
for num in b:
if num in set_a:
result.append(num)
set_a.remove(num)
return result
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated