If there's anything I can do to help you guys leave a comment below and lmk!
@rosonerri-faithful3 жыл бұрын
i wrote your code on leetcode but it isnt executing
@stanleymakori7772 жыл бұрын
Why must index always start from 1 ?
@haithemhtm10 ай бұрын
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Ай бұрын
@@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Ай бұрын
@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
@anindita28165 жыл бұрын
Damn, it's so simple and yet I was scratching my head!
@HarshKumar-sz2lo4 жыл бұрын
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-hd4sl4 жыл бұрын
Harsh Kumar your array is not sorted and the question said sorted array
@willturner34404 жыл бұрын
@@HarshKumar-sz2lo first sort the array
@koustubhmokashi40473 жыл бұрын
@@HarshKumar-sz2lo array is sorted and yours is not
@Sara-qf3ev3 жыл бұрын
@@HarshKumar-sz2lo it is a sorted one
@Deeepfactsss6 жыл бұрын
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.
@KevinNaughtonJr6 жыл бұрын
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 Жыл бұрын
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.
@MohiyuddinShaikh3 жыл бұрын
Just started my journey of mastering DSA, I missed out the main hint "sorted array". Learned a lot from this video, Subscribed!
@BessedDrest4 жыл бұрын
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.
@sagardafle3 жыл бұрын
Such an elegant solution to such simple yet tricky question. Thanks Kevin!
@abhilpnYT2 жыл бұрын
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Ай бұрын
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
@Randomisticful2 жыл бұрын
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.
@robertotorres85663 жыл бұрын
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?
@turskyi3 жыл бұрын
In production you would do that, but on leetcode, separation slowing performance as I see
@aikeber19844 жыл бұрын
Walmart lab is actually pretty famous in tech world
@khurshidallam39665 жыл бұрын
You sound like Sherlock Holmes with the way you said "interesting". :)
@KevinNaughtonJr5 жыл бұрын
Hahah I hope that's a good thing?!!?
@khurshidallam39665 жыл бұрын
@@KevinNaughtonJr haha. It's! Feels like we are solving some high profile crime case with you.
@KevinNaughtonJr5 жыл бұрын
@@khurshidallam3966 hahahah in that case let's keep solving these crimes!!!! :)
@TrenBlack5 жыл бұрын
You remind me of Dylan Mckenna
@SaulLee663 жыл бұрын
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.
@alammahtab084 жыл бұрын
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-kestler38683 жыл бұрын
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
@johnsoto71123 жыл бұрын
how does index return a list?
@z41n703 жыл бұрын
I don't understand why the function says to return an int but the description says to return an int[] ?
@arturonevarez7927 ай бұрын
It says return k, k represents the index I guess
@nathsai165810 ай бұрын
Umm, why is Walmart a "Random" company?
@mostinho75 жыл бұрын
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)
@seriusthechemist342 жыл бұрын
Thanks for the help! How come it doesn’t work for the assignment with nums[index+1] but it does for nums[index++]?
@vrushankdoshi78134 жыл бұрын
Hi Kevin, How can we do this in O(n) and O(1) without modifying the array ?
@JamieDawsonCodes Жыл бұрын
This video got you a subscriber! Thanks for this!
@XAVIER-rx9qn8 ай бұрын
Created 5 Years Ago, When I was in standard 12th. I am learning this toady. Am i way too late ?
@soumyasengupta70186 жыл бұрын
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. 😀
@KevinNaughtonJr6 жыл бұрын
Hey Soumya! Thanks for the input, I'll try and start throwing some harder problems into the mix!
@Chino508634864 жыл бұрын
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).
@karan1501913 жыл бұрын
This solution does not works when submitted at leet code. Gives "heap-buffer-overflow" Error.
@arnavraj34153 жыл бұрын
any fix for that ?
@arturonevarez7927 ай бұрын
You probably typo in the for condition, it's i < nums.length - 1
@collwyr4 жыл бұрын
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.
@samvid742 жыл бұрын
Kevin can you do thecsane problem with removing all duplicates (dont keep any duplicates at all)
@airysm5 жыл бұрын
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
@KevinNaughtonJr5 жыл бұрын
I actually work at a start up :) I hope you're enjoying the channel so far!
@karthikkottugumada89304 жыл бұрын
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 :)
@6rake93 жыл бұрын
What would you change if we wanted the output returned as an array int[ ] instead of an int variable?
@kevinrodriguez74032 ай бұрын
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"?
@domkatbess3224 жыл бұрын
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??
@KevinNaughtonJr4 жыл бұрын
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!!!
@saidurgasrividyaupadhyayul46754 жыл бұрын
Hello Kevin...I think the output for this solution will be 1 for an empty array but should be 0 as per my understanding.
@rajatsemwal18654 жыл бұрын
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!
@KevinNaughtonJr4 жыл бұрын
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
@rajatsemwal18654 жыл бұрын
@@KevinNaughtonJr That would be awesome. but can you solve my this query here please?
@KevinNaughtonJr4 жыл бұрын
Rajat Semwal the index variable tells you where to place the next unique item
@rajatsemwal18654 жыл бұрын
@@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?
@omerturkakin2625 жыл бұрын
Did you see that you are just returning the array index numbers 1 and 2 . It is like a jog
@davidhere_234 жыл бұрын
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.
@pradeeppatwa722 жыл бұрын
hey david how are you doing now, what level of coder are you now...?
@deletrious3 жыл бұрын
no way this is the third most asked question at facebook
@MunniDivya4 жыл бұрын
Thanks for a simple and easy method .
@lukkash4 жыл бұрын
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 :)
@RatioBozo692 жыл бұрын
This was my approach on C++ for this problem: class Solution { public: int removeDuplicates(vector& nums) { set a; for(int i=0; i
@bhattyash152 жыл бұрын
what is the time and space complexity here for this approach?
@arturonevarez7927 ай бұрын
O(n)
@priyankahegde64415 жыл бұрын
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 ??
@LetszGoo3 жыл бұрын
AT 4:21 i never had this expression when i got wrong answer😂😁
@kushalpatil3 жыл бұрын
the return type is int then how it's returning int array??
@vk16184 жыл бұрын
Couldn't think of a good solution
@sathyasai80264 жыл бұрын
Hey @kevin I love your videos, could u please make a video on max points on a line in leetcode
@TheNickcharlie6 жыл бұрын
Hey Kevin, thanks for all the videos, they're a huge help! Could we get in contact, I'd love to ask some questions
@KevinNaughtonJr6 жыл бұрын
Anytime! Yeah definitely, wanna shoot me an email and we can find a time to talk?
@TheNickcharlie6 жыл бұрын
Thanks Kevin! Could you please give me your email?
@TheNickcharlie6 жыл бұрын
thanks, just sent you an email! I appreciate it Kevin :)
@amitsaurabh99484 жыл бұрын
This doesnt work on interviewbit
@digishreshamwala17663 жыл бұрын
can you solve this using PHP?
@arunsagarsa36132 жыл бұрын
Thank you. If possible open source contribution video maybe…
@jaydeee17264 жыл бұрын
how to store it in a new declared array?
@nadaalqahtani23414 жыл бұрын
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-lx2xw4 жыл бұрын
great explanations on all your videos , however, Line 6 explanation is unclear to me. Could you please explain that one more time , thank you
@ishantyadav11343 жыл бұрын
same concern ,,how will the first element of array be printed
@tanishktripathi87733 жыл бұрын
Its simply a trick which we can observe when we manually trace the algorithm
@jatinkumar44104 жыл бұрын
Thanks for the help...
@eeshanchanpura33562 жыл бұрын
Runtime error for me
@kubersharma19714 жыл бұрын
THANKS FOR THAT MUCH EASE
@rahulbalan4 жыл бұрын
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....
@aliquewilliams30805 жыл бұрын
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.
@arnav975 жыл бұрын
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.
@espressothoughts5 жыл бұрын
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.
@Chino508634864 жыл бұрын
@@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.