LeetCode 26: Remove Duplicates from Sorted Array - Interview Prep Ep 45

Поделиться
HTML-код
  • Опубликовано: 10 фев 2025
  • ⭐ Shop on Amazon to support me: www.amazon.com...
    ⭐ NordVPN to protect your online privacy: go.nordvpn.net...
    ⭐ NordPass to help manage all of your passwords: go.nordpass.io...
    LeetCode 26. Remove Duplicates from Sorted Array
    ⭐ Support my channel and connect with me:
    / @fishercoder
    Clarification:
    Confused why the returned value is an integer but your answer is an array?
    Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
    Internally you can think of this:
    // nums is passed in by reference. (i.e., without making a copy)
    int len = removeDuplicates(nums);
    // any modification to nums in your function would be known by the caller.
    // using the length returned by your function, it prints the first len elements.
    for (int i = 0; i smaller than len; i++) {
    print(nums[i]);
    }
    Problem link: leetcode.com/p...
    Solution explained:
    1. We can apply the two pointer technique: one slow pointer pointing towards the one that's the last known distinct element, one fast pointer keeping moving towards the end of the array to scan for the next possible unique element.
    2. Finally, we just need to return the slow pointer position plus one.
    // TOOLS THAT I USE:
    ○ Memory Foam Set Keyboard Wrist Rest Pad - amzn.to/3cOGOAj
    ○ Electric Height Adjustable Standing Desk - amzn.to/2S9YexJ
    ○ Apple Magic Keyboard (Wireless, Rechargable) - amzn.to/36gy5FJ
    ○ Apple Magic Trackpad 2 (Wireless, Rechargable) - amzn.to/36ltimu
    ○ Apple MacBook Pro - amzn.to/30iSvKE
    ○ All-In One Printer - amzn.to/34etmSi
    ○ Apple AirPods Pro - amzn.to/2GpVYQf
    ○ My new favorite Apple Watch - amzn.to/2EIIUFd
    // MY FAVORITE BOOKS:
    ○ Introduction to Algorithms - amzn.to/36hxHXD
    ○ Designing Data-Intensive Applications - amzn.to/2S7snOg
    ○ Head First Java - amzn.to/2ScLDKa
    ○ Design Patterns - amzn.to/2SaGeU2
    Follow me on Github for complete LeetCode solutions: github.com/fis...
    Support me on Patreon: / fishercoder
    My ENTIRE Programming Equipment and Computer Science Bookshelf:
    www.amazon.com...
    And make sure you subscribe to my channel!
    Your comments/thoughts/questions/advice will be greatly appreciated!
    #softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures

Комментарии • 40

  • @Yakuza13329
    @Yakuza13329 Год назад +2

    It took me a while to realize this wasn't javaScript. It's crazy how similar Java's syntax is.

  • @akalrove4834
    @akalrove4834 3 года назад +3

    This is such a beautiful logic. We use the slow/fast pointer a lot in linkedlist so this is very good for that also.

  • @kelvinnguyen733
    @kelvinnguyen733 3 года назад +1

    Have my interview for Amazon soon, your channel is a blessing.

  • @ramonsanchez8815
    @ramonsanchez8815 3 года назад +1

    This made a lot more sense than the other tutorials thanks so much

  • @sugandhm2666
    @sugandhm2666 3 года назад +1

    Beautiful explanation using two pointer technique to solve this problem. Keep posting more brother

  • @prakashnavin6245
    @prakashnavin6245 2 года назад +1

    very great explanation sir keep it up

  • @AmitSingh-xg1tm
    @AmitSingh-xg1tm 3 года назад +1

    Such a simple and beautiful explanation. Superlike👍🏻

  • @vinaykaushikinvisble
    @vinaykaushikinvisble 2 года назад +1

    very nice explanation.

  • @srini580
    @srini580 3 года назад +1

    best explanation ever!

  • @yunlongjia5380
    @yunlongjia5380 3 года назад +1

    Thank you again!

  • @marshalldteach7797
    @marshalldteach7797 4 года назад +2

    great explanation

  • @ashenone5774
    @ashenone5774 3 года назад +1

    thank you! that helped a lot!

  • @prathishmurugan990
    @prathishmurugan990 4 года назад +2

    good explanation!

  • @iEatCarKeys
    @iEatCarKeys 2 года назад +1

    Not sure if I'll get a reply since this video is 2 years old but I have a question:
    If you switch nums[++i] to nums[i++] OR nums[ i + 1] why wouldn't it work?

    • @muhammadumair3258
      @muhammadumair3258 2 года назад

      i believe it would replace the slow pointer with the new value if you do i++ it uses the previous value of i then incriments it.

  • @sye119
    @sye119 4 года назад +1

    thanks, great explanation as always, just one problem in the description, the problem link is pointing to the Pascal's Triangle lol

  • @nardizena6641
    @nardizena6641 4 года назад +1

    Oh man, after fighting through the rubbish pile of Indian explanations made in android phones... I get you. Subscribed.

  • @fernandotanase114
    @fernandotanase114 2 года назад

    How long did it take you to build an intuition to this. I've been trying to learn Algos. & Data Struc. for the longest but I'm still dirt at it.

    • @FisherCoder
      @FisherCoder  2 года назад +1

      It takes quite some time before the ideas really sink in. Just be patient and hang in there! :)

  • @victoriakisel2004
    @victoriakisel2004 3 года назад +1

    It doesn't work if first two numbers of an array are the same. It only works by adding if statement before the for loop:
    int print(int arr[], int size) {
    int j = 0, k = 1;
    if (arr[0] == arr[k]) {
    cout

  • @abhiram6087
    @abhiram6087 3 года назад

    its a great explanation but why don't we use the second pointer from the end of the array

  • @bhavyashah1775
    @bhavyashah1775 4 года назад +1

    Hello Fisher, why did you return i + 1 and not just i ? can you let me know?

    • @thengonFTW
      @thengonFTW 4 года назад +2

      Because arrays are 0 based and i is a pointer used in the array. After the last swap the value of i is 4. But because arrays are 0 based we need to add one to i to get the length of the array

    • @jinhuidu8345
      @jinhuidu8345 4 года назад +2

      Because the slow pointer i started with 0 when the length of the array is 1. Meaning, array.length = i + 1.

  • @diproy9363
    @diproy9363 4 года назад

    nice explanation but you could've used a sketchpad i.e. jamboard/miro/paint.

  • @louischou6780
    @louischou6780 3 года назад

    why did you use ++i instead of i++ in line 6?

    • @sayan1193
      @sayan1193 3 года назад

      Because we have to change the value of slow pointer + 1 instead of slow pointer

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII 3 года назад

    Why so many dislikes on leetcode?