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
It took me a while to realize this wasn't javaScript. It's crazy how similar Java's syntax is.
This is such a beautiful logic. We use the slow/fast pointer a lot in linkedlist so this is very good for that also.
Have my interview for Amazon soon, your channel is a blessing.
This made a lot more sense than the other tutorials thanks so much
You're welcome!
Beautiful explanation using two pointer technique to solve this problem. Keep posting more brother
very great explanation sir keep it up
Thanks and welcome!
Such a simple and beautiful explanation. Superlike👍🏻
Thank you! Cheers!
very nice explanation.
Glad you liked it!
best explanation ever!
Glad you think so!
Thank you again!
Any time!
great explanation
Glad it was helpful!
thank you! that helped a lot!
Glad it helped!
good explanation!
Glad you liked it!
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?
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.
thanks, great explanation as always, just one problem in the description, the problem link is pointing to the Pascal's Triangle lol
Updated, thanks! :)
Oh man, after fighting through the rubbish pile of Indian explanations made in android phones... I get you. Subscribed.
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.
It takes quite some time before the ideas really sink in. Just be patient and hang in there! :)
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
its a great explanation but why don't we use the second pointer from the end of the array
Because array is already sorted.
Hello Fisher, why did you return i + 1 and not just i ? can you let me know?
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
Because the slow pointer i started with 0 when the length of the array is 1. Meaning, array.length = i + 1.
nice explanation but you could've used a sketchpad i.e. jamboard/miro/paint.
why did you use ++i instead of i++ in line 6?
Because we have to change the value of slow pointer + 1 instead of slow pointer
Why so many dislikes on leetcode?