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); } }
@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
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.
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
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.
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. 😀
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.
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?
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 ?
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
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
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??
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!!!
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)
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; }
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
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!
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
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.
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 :)
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.
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"?
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 :)
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)
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....
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.
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.
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.
@@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.