Intersection of Two Arrays II Leetcode daily challenge || Intuition + Code + Explanation

preview_player
Показать описание
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Explanation: [9,4] is also accepted.
Рекомендации по теме
Комментарии
Автор

The explanation got me a success on Leetcode with this problem 🔥🔥 Thanks a ton!

aadarshlalchandani
Автор

you know what's more OP than the explanation, the kid behind her

whyrohit
Автор

I did not find the correct explanation for duplicate elements, thanks a lot it helped

pranjalck
Автор

This is the only video that helped me, Thank you!

tusharnain
Автор

Question has been changed now. In the first example the output should be [2] and not [2, 2].

abhigyanmanasvi
Автор

you are good given your age! keep it up.👍

Shva
Автор

If input arrays are sorted
Two pointer is an optimal approach

beinghappy
Автор

This was such an intuitive way to use hashmap! Great job

MrKB_SSJ
Автор

OH ! The Explaination is so Amazing !
Thanks, Alisha

zeyadalbadawy
Автор

the approach is good but Im confusing how the code is working, please trace the code so that we could understand easily. Thanks for your effort for making edu contents

SanthoshaK-pxrq
Автор

can you take example in real world
this make your explanation is so effective
thanks a lot
I wish happy journey

adityasharma-iqxk
Автор

very good intuition explanation! HashSet first come to my mind, but after trying, I find that it only prints out the unique elements which is no the desired answer

gloriadai
Автор

In 2nd for loop hum bs yh bhi to kr skte the n If(m[num2[i]]){ vector.push_back(num2[i]) ;
m[num2[i]]--;
}

Mtlb itni saari conditions likhna nhi pdhta

AmanSingh-ffim
Автор

time complexity = nlogn
space complexity= n
am i right ?

updatedtalk
Автор

Didi can u pls help why if(mpp[nums2[i]]==1) in this line it is equal to 1 what does it indicate pls help🙏
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> mpp;
vector<int> ans;

// putting only unique values in map
for(auto i: nums1)if(mpp[i]==0)mpp[i]++;

// now if nums2[i] is present in the map -
// push it to vector & increment it's value in map to avoid repeatating element in nums2
for(int i = 0 ; i<nums2.size(); i++){
if(mpp[nums2[i]]==1){
ans.push_back(nums2[i]);
mpp[nums2[i]]++;
}
}
return ans;
}
};

AayushSharma-whuy
Автор

Hi...thnx for the solution and wokrs like charm but sad part is...this is the only way to resolve this....no different ideas....everyone is solving based on this logic only.

ashishp-ci
Автор

Thanks for the explanation, it helped!

sudershansingh
Автор

map, set se to nursary ke bache b kr lete h binary search se kro koi

yogeshyts
Автор

What is the time and space complexity ?

rohanmahajan
Автор

Please provide java code of it, Thanks in advance.

rohan