Remove Duplicates from Sorted Array

  Рет қаралды 76,210

Kevin Naughton Jr.

Kevin Naughton Jr.

Күн бұрын

Пікірлер: 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 10 ай бұрын
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 Ай бұрын
@@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 Ай бұрын
@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
@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 4 жыл бұрын
@@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
@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!
@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.
@sagardafle
@sagardafle 3 жыл бұрын
Such an elegant solution to such simple yet tricky question. Thanks Kevin!
@abhilpnYT
@abhilpnYT 2 жыл бұрын
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 Ай бұрын
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
@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 3 жыл бұрын
In production you would do that, but on leetcode, separation slowing performance as I see
@aikeber1984
@aikeber1984 4 жыл бұрын
Walmart lab is actually pretty famous in tech world
@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!!!! :)
@TrenBlack
@TrenBlack 5 жыл бұрын
You remind me of Dylan Mckenna
@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.
@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; }
@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
@johnsoto7112
@johnsoto7112 3 жыл бұрын
how does index return a list?
@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 7 ай бұрын
It says return k, k represents the index I guess
@nathsai1658
@nathsai1658 10 ай бұрын
Umm, why is Walmart a "Random" company?
@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)
@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++]?
@vrushankdoshi7813
@vrushankdoshi7813 4 жыл бұрын
Hi Kevin, How can we do this in O(n) and O(1) without modifying the array ?
@JamieDawsonCodes
@JamieDawsonCodes Жыл бұрын
This video got you a subscriber! Thanks for this!
@XAVIER-rx9qn
@XAVIER-rx9qn 8 ай бұрын
Created 5 Years Ago, When I was in standard 12th. I am learning this toady. Am i way too late ?
@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!
@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).
@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 7 ай бұрын
You probably typo in the for condition, it's i < nums.length - 1
@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.
@samvid74
@samvid74 2 жыл бұрын
Kevin can you do thecsane problem with removing all duplicates (dont keep any duplicates at all)
@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!
@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 :)
@6rake9
@6rake9 3 жыл бұрын
What would you change if we wanted the output returned as an array int[ ] instead of an int variable?
@kevinrodriguez7403
@kevinrodriguez7403 2 ай бұрын
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"?
@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!!!
@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.
@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?
@omerturkakin262
@omerturkakin262 5 жыл бұрын
Did you see that you are just returning the array index numbers 1 and 2 . It is like a jog
@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...?
@deletrious
@deletrious 3 жыл бұрын
no way this is the third most asked question at facebook
@MunniDivya
@MunniDivya 4 жыл бұрын
Thanks for a simple and easy method .
@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 :)
@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
@bhattyash15
@bhattyash15 2 жыл бұрын
what is the time and space complexity here for this approach?
@arturonevarez792
@arturonevarez792 7 ай бұрын
O(n)
@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 ??
@LetszGoo
@LetszGoo 3 жыл бұрын
AT 4:21 i never had this expression when i got wrong answer😂😁
@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
@sathyasai8026
@sathyasai8026 4 жыл бұрын
Hey @kevin I love your videos, could u please make a video on max points on a line in leetcode
@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 :)
@amitsaurabh9948
@amitsaurabh9948 4 жыл бұрын
This doesnt work on interviewbit
@digishreshamwala1766
@digishreshamwala1766 3 жыл бұрын
can you solve this using PHP?
@arunsagarsa3613
@arunsagarsa3613 2 жыл бұрын
Thank you. If possible open source contribution video maybe…
@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)
@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
@jatinkumar4410
@jatinkumar4410 4 жыл бұрын
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 5 жыл бұрын
Walmart is not random!
@R9000S
@R9000S 4 жыл бұрын
Yup they pay the highest
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
12:19
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 286 М.
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 17 МЛН
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 8 МЛН
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН
Remove Duplicates from Sorted Array - Leetcode 26 - Python
10:38
Decode Ways
7:31
Kevin Naughton Jr.
Рет қаралды 105 М.
Remove Duplicates From Sorted Array | Brute | Optimal
10:47
take U forward
Рет қаралды 132 М.
15 Years Writing C++ - Advice for new programmers
4:04
SyncMain
Рет қаралды 1,3 МЛН
⚡️NEWS | RUBLE COLLAPSE | STRIKE ON CRIMEA | PUTIN IN KAZAKHSTAN
10:34
Ходорковский LIVE
Рет қаралды 156 М.
Word Search
8:46
Kevin Naughton Jr.
Рет қаралды 139 М.