Thank you for doing all of these. I noticed that you always loop through an array when adding to a set. Is there any speed difference instead of just doing new Set(array)?
Nope. No speed difference that I know of. I mainly do it because I know most people watching my videos aren’t using JavaScript, so I try to use methods that are recognizable across different languages.
// this beats 74.79% of the leetcode solutions var intersection = function(nums1, nums2) { return Array.from(new Set(nums1.filter(each => nums2.includes(each)))) };
var intersection = function(nums1, nums2) { let finalResult = [] let nums1_uniques = new Set(nums1); for(let each of nums2){ if(!finalResult.includes(each) && nums1_uniques.has(each)){ finalResult.push(each) } } return finalResult };
I am just curious why have you put the second array inside a set? As soon have you found one matching element in set, that element should be erased from set, and that way you can eliminate the need to storing the second array in a set.
Thanks for such awesome explanation.I doubt though....can we iterate over each element of set and check if that element is in second array...if no then we delete that element from set....and the resultant elements in the set will be our solution.Is this approach right?
If there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!
Can you make video on 350 code intersection of two arrays ||. Your explanation is too good 👍 thank you soo much
@@mounisri9797 +1
all I can say is......thank god for your youtube channel
This is super clear to me now, thank you! You are the best my friend!
Thanks!
Thank you for doing all of these. I noticed that you always loop through an array when adding to a set. Is there any speed difference instead of just doing new Set(array)?
Nope. No speed difference that I know of. I mainly do it because I know most people watching my videos aren’t using JavaScript, so I try to use methods that are recognizable across different languages.
// this beats 74.79% of the leetcode solutions
var intersection = function(nums1, nums2) {
return Array.from(new Set(nums1.filter(each => nums2.includes(each))))
};
var intersection = function(nums1, nums2) {
let finalResult = []
let nums1_uniques = new Set(nums1);
for(let each of nums2){
if(!finalResult.includes(each) && nums1_uniques.has(each)){
finalResult.push(each)
}
}
return finalResult
};
Two pointer approach is efficient I guess since that has constant space
I am just curious why have you put the second array inside a set? As soon have you found one matching element in set, that element should be erased from set, and that way you can eliminate the need to storing the second array in a set.
var intersection = function(nums1, nums2) {
return Array.from(new Set(nums1.filter(each => nums2.includes(each))))
};
Thanks for such awesome explanation.I doubt though....can we iterate over each element of set and check if that element is in second array...if no then we delete that element from set....and the resultant elements in the set will be our solution.Is this approach right?
Checking if that element is present in an array will be costly operation O(length of array)
@@SuperSam4y0u right,thanks mate