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

preview_player
Показать описание

LeetCode 26. Remove Duplicates from Sorted Array

⭐ Support my channel and connect with me:

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

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:

// MY FAVORITE BOOKS:

My ENTIRE Programming Equipment and Computer Science Bookshelf:

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.

Yakuza
Автор

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

akalrove
Автор

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

kelvinnguyen
Автор

very great explanation sir keep it up

prakashnavin
Автор

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

ramonsanchez
Автор

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

sugandhm
Автор

Such a simple and beautiful explanation. Superlike👍🏻

AmitSingh-xgtm
Автор

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?

iEatCarKeys
Автор

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

sye
Автор

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.

fernandotanase
Автор

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 << arr[0] << " ";
}
for (int i = 1; i < size; i++)
{
if (arr[j] != arr[i]) {
arr[++j] = arr[i];
cout << arr[j] << " ";
}

}
return j+1;

victoriakisel
Автор

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

bhavyashah
Автор

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

abhiram
Автор

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

louischou
Автор

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

nardizena
Автор

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

diproy