Intersection of Two Arrays | LeetCode 349 | Lyft Coding Interview Tutorial

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 17

  • @TerribleWhiteboard
    @TerribleWhiteboard  Před 4 lety +12

    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!

    • @mounisri9797
      @mounisri9797 Před 3 lety

      Can you make video on 350 code intersection of two arrays ||. Your explanation is too good 👍 thank you soo much

    • @TekkenRoom
      @TekkenRoom Před rokem

      @@mounisri9797 +1

  • @ruthylevi9804
    @ruthylevi9804 Před 2 lety +1

    all I can say is......thank god for your youtube channel

  • @ChrisTian-ox5nr
    @ChrisTian-ox5nr Před 3 lety +3

    This is super clear to me now, thank you! You are the best my friend!

  • @thomasvaeth
    @thomasvaeth Před 4 lety +13

    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)?

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +16

      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.

  • @shadmanmartinpiyal4057

    // this beats 74.79% of the leetcode solutions
    var intersection = function(nums1, nums2) {
    return Array.from(new Set(nums1.filter(each => nums2.includes(each))))
    };

  • @shadmanmartinpiyal4057

    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
    };

  • @carefree_ladka
    @carefree_ladka Před 2 lety

    Two pointer approach is efficient I guess since that has constant space

  • @goforshahriar
    @goforshahriar Před 3 lety +1

    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.

    • @shadmanmartinpiyal4057
      @shadmanmartinpiyal4057 Před rokem

      var intersection = function(nums1, nums2) {
      return Array.from(new Set(nums1.filter(each => nums2.includes(each))))
      };

  • @tejaskaria9541
    @tejaskaria9541 Před 3 lety +1

    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?

    • @SuperSam4y0u
      @SuperSam4y0u Před 3 lety +1

      Checking if that element is present in an array will be costly operation O(length of array)

    • @tejaskaria9541
      @tejaskaria9541 Před 3 lety

      @@SuperSam4y0u right,thanks mate