Тёмный

Remove Duplicates from Sorted Array 

Kevin Naughton Jr.
Подписаться 138 тыс.
Просмотров 76 тыс.
50% 1

Опубликовано:

 

26 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 104   
@KevinNaughtonJr
@KevinNaughtonJr 6 лет назад
If there's anything I can do to help you guys leave a comment below and lmk!
@rosonerri-faithful
@rosonerri-faithful 3 года назад
i wrote your code on leetcode but it isnt executing
@stanleymakori777
@stanleymakori777 2 года назад
Why must index always start from 1 ?
@haithemhtm
@haithemhtm 9 месяцев назад
what's the problem with this please, it shows 3 not 4 !!!: public class RemoveDuplicateTableau { public static int removeDuplicate(int[] nums) { int result = 1; for (int i = 0 ; i < nums.length - 1 ; i++){ if(nums[i] != nums[i+1]){ result++; } } return result; } public static void main(String[] args){ int[] s = {0,0,1,1,1,2,3,3}; int r = removeDuplicate(s); System.out.println(r); } }
@sharma-raju
@sharma-raju 11 дней назад
@@haithemhtm - Hey I see you are not updating the array's value i think inside the if condition you should use nums[result++] = nums[i+1];
@sharma-raju
@sharma-raju 11 дней назад
@KevinNaughtonJr - I believe it is not removing the duplicate element instead it is shifting unique element in the left side and keeping rest all the element (from index to length of array) same as it is
@Deeepfactsss
@Deeepfactsss 6 лет назад
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.
@KevinNaughtonJr
@KevinNaughtonJr 6 лет назад
Thanks Michael I'm happy to hear the videos are helpful! If there's anything I can do to make them better please let me know!
@anindita2816
@anindita2816 5 лет назад
Damn, it's so simple and yet I was scratching my head!
@HarshKumar-sz2lo
@HarshKumar-sz2lo 4 года назад
It's wrong Actually. Give input as : int nums[]= {1, 2, 5, 5, 6, 6, 7, 2}; Then It wont compare for last index and O/P will give 1 2 5 6 7 2 which is wrong Actually
@user-hd4sl
@user-hd4sl 4 года назад
Harsh Kumar your array is not sorted and the question said sorted array
@willturner3440
@willturner3440 3 года назад
@@HarshKumar-sz2lo first sort the array
@koustubhmokashi4047
@koustubhmokashi4047 3 года назад
@@HarshKumar-sz2lo array is sorted and yours is not
@Sara-qf3ev
@Sara-qf3ev 3 года назад
@@HarshKumar-sz2lo it is a sorted one
@nacimhoc
@nacimhoc Год назад
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.
@MohiyuddinShaikh
@MohiyuddinShaikh 3 года назад
Just started my journey of mastering DSA, I missed out the main hint "sorted array". Learned a lot from this video, Subscribed!
@BessedDrest
@BessedDrest 4 года назад
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.
@khurshidallam3966
@khurshidallam3966 5 лет назад
You sound like Sherlock Holmes with the way you said "interesting". :)
@KevinNaughtonJr
@KevinNaughtonJr 5 лет назад
Hahah I hope that's a good thing?!!?
@khurshidallam3966
@khurshidallam3966 5 лет назад
@@KevinNaughtonJr haha. It's! Feels like we are solving some high profile crime case with you.
@KevinNaughtonJr
@KevinNaughtonJr 5 лет назад
@@khurshidallam3966 hahahah in that case let's keep solving these crimes!!!! :)
@soumyasengupta7018
@soumyasengupta7018 6 лет назад
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. 😀
@KevinNaughtonJr
@KevinNaughtonJr 6 лет назад
Hey Soumya! Thanks for the input, I'll try and start throwing some harder problems into the mix!
@sagardafle
@sagardafle 3 года назад
Such an elegant solution to such simple yet tricky question. Thanks Kevin!
@Randomisticful
@Randomisticful 2 года назад
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.
@robertotorres8566
@robertotorres8566 3 года назад
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?
@turskyi
@turskyi 2 года назад
In production you would do that, but on leetcode, separation slowing performance as I see
@SaulLee66
@SaulLee66 3 года назад
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.
@abhilpnYT
@abhilpnYT Год назад
Does this code really "remove the duplicate" Or just to take the count of the "Unique Element". An attempt to print array after "removal" and you will still see the last element "duplicated". Bit confusing on title and expected answer details ? Any suggestions ?
@sharma-raju
@sharma-raju 11 дней назад
Yeah I think it is not removing the duplicate element instead it is shifting unique element in the left side and keeping rest all the element (from index to length of array) same as it is
@mechaelbrun-kestler3868
@mechaelbrun-kestler3868 3 года назад
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
@aikeber1984
@aikeber1984 3 года назад
Walmart lab is actually pretty famous in tech world
@TrenBlack
@TrenBlack 5 лет назад
You remind me of Dylan Mckenna
@domkatbess322
@domkatbess322 4 года назад
Hi @Kevin, I am a huge fan. I like the way you systematically approach every problem. Your explanations are superb, sometimes it makes me even believe I am in your mind just by hearing you explain it. But when on my own, i find it difficult to think of the problems the way you do. Do you think you can be of help??
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
Thanks so much! Don't worry about having trouble with these problems, they're tough to solve! I actually just launched an interviewing service to help with this exact kind of thing, it's called The Daily Byte: thedailybyte.dev/. Joining my service will definitely help you understand how to solve these problems on your own by teaching you one problem at a time in an order that encourages learning. Each day you'll get a problem delivered to your email inbox and if you sign up for premium you'll get solutions the following day including a detailed walkthrough of how to solve the problem as well as an explanation of the runtime and space complexity. I'd join the annual tier if I were you! I hope this helps!!!
@XAVIER-rx9qn
@XAVIER-rx9qn 7 месяцев назад
Created 5 Years Ago, When I was in standard 12th. I am learning this toady. Am i way too late ?
@mostinho7
@mostinho7 5 лет назад
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)
@johnsoto7112
@johnsoto7112 3 года назад
how does index return a list?
@nathsai1658
@nathsai1658 9 месяцев назад
Umm, why is Walmart a "Random" company?
@seriusthechemist34
@seriusthechemist34 2 года назад
Thanks for the help! How come it doesn’t work for the assignment with nums[index+1] but it does for nums[index++]?
@z41n70
@z41n70 3 года назад
I don't understand why the function says to return an int but the description says to return an int[] ?
@arturonevarez792
@arturonevarez792 6 месяцев назад
It says return k, k represents the index I guess
@alammahtab08
@alammahtab08 4 года назад
Good job, below is the solution using extra space and without using extra space. Using Extra Space: public static int removeDuplicates(int[] nums) { Set 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; }
@JamieDawsonCodes
@JamieDawsonCodes Год назад
This video got you a subscriber! Thanks for this!
@airysm
@airysm 5 лет назад
Hey I just came across your channel! I was wondering where do you work?(if you could share that lol) since you're really good at these interview questions I assume you work at a big n
@KevinNaughtonJr
@KevinNaughtonJr 5 лет назад
I actually work at a start up :) I hope you're enjoying the channel so far!
@Chino50863486
@Chino50863486 4 года назад
The prompt wasn't clear in my opinion. I thought I had to remove the repeated elements(or clear them to 0) But was unable to do so in O(n).
@samvid74
@samvid74 2 года назад
Kevin can you do thecsane problem with removing all duplicates (dont keep any duplicates at all)
@rajatsemwal1865
@rajatsemwal1865 4 года назад
I am currently an undergrad student and i am currently practicing for DSA on leetcode. I was really scratching my head on this question and damn, you explained it so smoothly. But i am still stuck on the index variable, I am little confused about it. Can you explain it better? Thanks. I subscribed your channel too!
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
Rajat Semwal thanks! If you need help with these kinds of problems I strongly recommend signing up for a premium plan on the interviewing service I created where I teach you how to solve these problems! thedailybyte.dev/?ref=kevin
@rajatsemwal1865
@rajatsemwal1865 4 года назад
@@KevinNaughtonJr That would be awesome. but can you solve my this query here please?
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
Rajat Semwal the index variable tells you where to place the next unique item
@rajatsemwal1865
@rajatsemwal1865 4 года назад
@@KevinNaughtonJr So that's why, when you kept the index at 1, you already knew that at index 0, it will already have a unique value, am I correct?
@priyankahegde6441
@priyankahegde6441 5 лет назад
Your videos are really great and I had watched 40 videos till now! Thanks a lot for helping us out 😀can you please make some videos on linked list ??
@vrushankdoshi7813
@vrushankdoshi7813 4 года назад
Hi Kevin, How can we do this in O(n) and O(1) without modifying the array ?
@saidurgasrividyaupadhyayul4675
@saidurgasrividyaupadhyayul4675 4 года назад
Hello Kevin...I think the output for this solution will be 1 for an empty array but should be 0 as per my understanding.
@6rake9
@6rake9 3 года назад
What would you change if we wanted the output returned as an array int[ ] instead of an int variable?
@collwyr
@collwyr 4 года назад
as annoying as it is to say, I literally could not wrap my head around this problem AT ALL!!!... I was looking at solutions and I was reading the code and it just felt wrong. you video made me realise that I was stuck in the mind frame of trying to return an array without any duplicates. rather than rearranging the array so the "first" section if you will is without duplicates and you return that length rather than the array length itself.
@karthikkottugumada8930
@karthikkottugumada8930 4 года назад
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 :)
@karan150191
@karan150191 3 года назад
This solution does not works when submitted at leet code. Gives "heap-buffer-overflow" Error.
@arnavraj3415
@arnavraj3415 3 года назад
any fix for that ?
@arturonevarez792
@arturonevarez792 6 месяцев назад
You probably typo in the for condition, it's i < nums.length - 1
@davidhere_23
@davidhere_23 4 года назад
Hi a newbie here, sorry but can you make a video to talk about the O(1) condition? I am not sure which indicator to see to make sure that our answer fulfills this condition...And there is also O(n) or O(nxn)..Basically, what I want to know is how do we know that the answer we provide fulfills the conditions given in the question? Thank you very much in advanced for any help. Much appreciated.
@pradeeppatwa72
@pradeeppatwa72 2 года назад
hey david how are you doing now, what level of coder are you now...?
@RatioBozo69
@RatioBozo69 2 года назад
This was my approach on C++ for this problem: class Solution { public: int removeDuplicates(vector& nums) { set a; for(int i=0; i
@kevinrodriguez7403
@kevinrodriguez7403 Месяц назад
in the for loop, why is it "nums.length - 1" i guess it has something to do with bounds? i don't understand how it helps for this problem, for example if we have an array [0][1][2][3], where is the "-1"?
@deletrious
@deletrious 3 года назад
no way this is the third most asked question at facebook
@omerturkakin262
@omerturkakin262 5 лет назад
Did you see that you are just returning the array index numbers 1 and 2 . It is like a jog
@bhattyash15
@bhattyash15 2 года назад
what is the time and space complexity here for this approach?
@arturonevarez792
@arturonevarez792 6 месяцев назад
O(n)
@MunniDivya
@MunniDivya 4 года назад
Thanks for a simple and easy method .
@sathyasai8026
@sathyasai8026 4 года назад
Hey @kevin I love your videos, could u please make a video on max points on a line in leetcode
@lukkash
@lukkash 4 года назад
Why Walmart? because if you pass programming test then you can work for them as a client advisor in IT products department having such a great knowledge :)
@TheNickcharlie
@TheNickcharlie 6 лет назад
Hey Kevin, thanks for all the videos, they're a huge help! Could we get in contact, I'd love to ask some questions
@KevinNaughtonJr
@KevinNaughtonJr 6 лет назад
Anytime! Yeah definitely, wanna shoot me an email and we can find a time to talk?
@TheNickcharlie
@TheNickcharlie 6 лет назад
Thanks Kevin! Could you please give me your email?
@TheNickcharlie
@TheNickcharlie 6 лет назад
thanks, just sent you an email! I appreciate it Kevin :)
@kushalpatil
@kushalpatil 3 года назад
the return type is int then how it's returning int array??
@vk1618
@vk1618 4 года назад
Couldn't think of a good solution
@digishreshamwala1766
@digishreshamwala1766 3 года назад
can you solve this using PHP?
@LetszGoo
@LetszGoo 3 года назад
AT 4:21 i never had this expression when i got wrong answer😂😁
@amitsaurabh9948
@amitsaurabh9948 4 года назад
This doesnt work on interviewbit
@arunsagarsa3613
@arunsagarsa3613 2 года назад
Thank you. If possible open source contribution video maybe…
@Priyanka-lx2xw
@Priyanka-lx2xw 4 года назад
great explanations on all your videos , however, Line 6 explanation is unclear to me. Could you please explain that one more time , thank you
@ishantyadav1134
@ishantyadav1134 3 года назад
same concern ,,how will the first element of array be printed
@tanishktripathi8773
@tanishktripathi8773 3 года назад
Its simply a trick which we can observe when we manually trace the algorithm
@jaydeee1726
@jaydeee1726 4 года назад
how to store it in a new declared array?
@nadaalqahtani2341
@nadaalqahtani2341 4 года назад
for python3 def Duplicates(nums, n): if n < 1: return nums i = 1 j = 1 t = [] for i in range(n): if nums[i] != nums[i-1]: t.append(nums[i]) i+=1 return t n = len(nums) nums = [0,0,1,1,2,3,3,4] Duplicates(nums, n)
@jatinkumar4410
@jatinkumar4410 3 года назад
Thanks for the help...
@eeshanchanpura3356
@eeshanchanpura3356 2 года назад
Runtime error for me
@kubersharma1971
@kubersharma1971 4 года назад
THANKS FOR THAT MUCH EASE
@rahulbalan
@rahulbalan 4 года назад
I spent about an hour on this question, and still got a TLE on submission! I was actually removing each and every duplicate element by left shifting the array... damn I suck balls at this....
@aliquewilliams3080
@aliquewilliams3080 5 лет назад
This is just a hack; he doesn't actually remove duplicates but ends up scrambling the input array. This isn't something one would implement in the real world.
@arnav97
@arnav97 5 лет назад
It's actually okay, that's what the question asked. Since it asks us to modify the array in-place, we don't have to take care of the elements beyond the returned length.
@espressothoughts
@espressothoughts 5 лет назад
He did remove it though. By reassigning it a new value, any time you reassign to an index you change it's value therefore losing (removing) it's previous value.
@Chino50863486
@Chino50863486 4 года назад
@@espressothoughts Yes, but what he meant is that he did not remove the extra(repeated) values from the array. But again, this is what the question asked, so its ok.
@fredwu6812
@fredwu6812 4 года назад
Walmart is not random!
@R9000S
@R9000S 4 года назад
Yup they pay the highest
Далее
An Entire Computer Science Degree in 11 Minutes
11:13
Просмотров 836 тыс.
Только ЕМУ это удалось
01:00
Просмотров 1,8 млн
This Algorithm is 1,606,240% FASTER
13:31
Просмотров 846 тыс.
Decode Ways
7:31
Просмотров 105 тыс.
The HARSH Reality of Working in Big Tech
8:42
Просмотров 22 тыс.
8 patterns to solve 80% Leetcode problems
7:30
Просмотров 409 тыс.
My 10 “Clean” Code Principles (Start These Now)
15:12
Remove Duplicates From Sorted Array | Brute | Optimal
10:47