Remove Duplicates from Sorted Array

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

If there's anything I can do to help you guys leave a comment below and lmk!

KevinNaughtonJr
Автор

Damn, it's so simple and yet I was scratching my head!

anindita
Автор

Love the videos. Just recently found your channel and it’s been nice to try to solve the problem before watching your solution. I love building apps but coding problems are something I’ve struggled with for a while. Watching your thought process tremendously helps. Keep up the videos Kevin.

Deeepfactsss
Автор

Just started my journey of mastering DSA, I missed out the main hint "sorted array". Learned a lot from this video, Subscribed!

MohiyuddinShaikh
Автор

Walmart is not a random company, their website is one of the most visited in the country, so a lot of optimization is going on.

nacimhoc
Автор

I was a little confused at first but after reading the notes in the examples a second time, this approach makes a ton of sense. The 'return the new length' requirement seems deceptive, as if they want you to return the new length of the array as a whole.

BessedDrest
Автор

Such an elegant solution to such simple yet tricky question. Thanks Kevin!

sagardafle
Автор

Walmart lab is actually pretty famous in tech world

aikeber
Автор

You sound like Sherlock Holmes with the way you said "interesting". :)

khurshidallam
Автор

Now the #26 question has been modified with a new requirement, the relative order should be maintained. His solution will not work any more, sadly.

SaulLee
Автор

Thanks but actually it failed when I tested it with multiple (more than 2 or 3) same numbers, yet in ascending order. My solution was to: have two outside-the-loop variables, index which was 0 and integer - "previous" who was a really big number. In the loop: if the current is not the same with previous the value at the index (remember, the one we sat to zero outside the loop) so nums[index] = nums[i]. Set the "previous" to the nums[index] and increment index by one, all of it in the same if statement.

Randomisticful
Автор

Good job, below is the solution using extra space and without using extra space.
Using Extra Space:
public static int removeDuplicates(int[] nums) {
Set<Integer> set = new HashSet<>();
int index = 0;
for(int i : nums){
if(!set.contains(i)){
set.add(i);
nums[index++] = i;
}
}
return index;
}


Without using extra space :
public int removeDuplicates(int[] nums) {
int index = 1;
for(int i = 1; i < nums.length; i++){
if(nums[i] != nums[i-1])
nums[index++] = nums[i];
}
return index;
}

alammahtab
Автор

I appreciate the help, but one thing I notice is I find it so hard to understand seeing two operations in one line: i++ and assignment. Why not break it on two separate lines for clarity?

robertotorres
Автор

Thanks. loops through the array and places the next new number it sees at the index, then increments the index. (skipping the first index because will always be unique)


1, 1, 2, 2, 3
should return 3

index = 1
for i=0:
nums[0] (1) != nums[1] false

for i=1
nums[1] (1) != nums[2] (2) true
nums[1] = nums[2] (array becomes 1, 2, 2, 2, 3)
index is now 2

for i=2
nums[2] (2) != nums[3] false

for i=3
nums[3] (2) != nums[4] (3) true
nums[2] = nums[4] (array becomes 1, 2, 3, 2, 3)

mostinho
Автор

You are not allowed to modify an array during a loop or you invalidate all the indices. So you have to mark which indices are to be removed and then remove them later

mechaelbrun-kestler
Автор

Hi kevin,
The videos are really nice and it gets you into the habit of solving problems online consistently over a long period which is really beneficial.
However can you move to some medium/hard related problems which require some additional insight and complexity.
It will really be helpful.
😀

soumyasengupta
Автор

Thank you Kevin, I really love your videos and the effort that you put in.
However, this would also need to take into account the boundary case of an empty array. C# OJ on LC fails at that testcase. I understand your intent was to explain the logic :)

karthikkottugumada
Автор

I don't understand why the function says to return an int but the description says to return an int[] ?

zn
Автор

This solution does not works when submitted at leet code. Gives "heap-buffer-overflow" Error.

karan
Автор

AT 4:21 i never had this expression when i got wrong answer😂😁

abhishekshrivastav