Remove Duplicates from Sorted Array

Поділитися
Вставка

КОМЕНТАРІ • 102

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 років тому +16

    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 5 місяців тому

      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);
      }
      }

  • @Deeepfactsss
    @Deeepfactsss 5 років тому +12

    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  5 років тому +1

      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 років тому +39

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

    • @HarshKumar-sz2lo
      @HarshKumar-sz2lo 3 роки тому

      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 3 роки тому +19

      Harsh Kumar your array is not sorted and the question said sorted array

    • @willturner3440
      @willturner3440 3 роки тому +3

      @@HarshKumar-sz2lo first sort the array

    • @koustubhmokashi4047
      @koustubhmokashi4047 3 роки тому +1

      @@HarshKumar-sz2lo array is sorted and yours is not

    • @Sara-qf3ev
      @Sara-qf3ev 2 роки тому

      @@HarshKumar-sz2lo it is a sorted one

  • @MohiyuddinShaikh
    @MohiyuddinShaikh 3 роки тому +1

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

  • @sagardafle
    @sagardafle 2 роки тому

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

  • @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.

  • @vrushankdoshi7813
    @vrushankdoshi7813 4 роки тому +1

    Hi Kevin,
    How can we do this in O(n) and O(1) without modifying the array ?

  • @6rake9
    @6rake9 3 роки тому

    What would you change if we wanted the output returned as an array int[ ] instead of an int variable?

  • @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 :)

  • @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++]?

  • @airysm
    @airysm 5 років тому +2

    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 років тому +1

      I actually work at a start up :) I hope you're enjoying the channel so far!

  • @priyankahegde6441
    @priyankahegde6441 4 роки тому

    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 ??

  • @mechaelbrun-kestler3868
    @mechaelbrun-kestler3868 2 роки тому

    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

  • @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.

  • @robertotorres8566
    @robertotorres8566 3 роки тому +2

    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

  • @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 ?

  • @soumyasengupta7018
    @soumyasengupta7018 5 років тому +3

    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  5 років тому +1

      Hey Soumya! Thanks for the input, I'll try and start throwing some harder problems into the mix!

  • @Chino50863486
    @Chino50863486 4 роки тому +3

    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).

  • @MunniDivya
    @MunniDivya 3 роки тому

    Thanks for a simple and easy method .

  • @domkatbess322
    @domkatbess322 4 роки тому +2

    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 роки тому +1

      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 4 місяці тому

    Created 5 Years Ago, When I was in standard 12th. I am learning this toady. Am i way too late ?

  • @Randomisticful
    @Randomisticful 2 роки тому +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.

  • @samvid74
    @samvid74 2 роки тому

    Kevin can you do thecsane problem with removing all duplicates (dont keep any duplicates at all)

  • @sathyasai8026
    @sathyasai8026 3 роки тому

    Hey @kevin I love your videos, could u please make a video on max points on a line in leetcode

  • @JamieDawsonCodes
    @JamieDawsonCodes Рік тому

    This video got you a subscriber! Thanks for this!

  • @khurshidallam3966
    @khurshidallam3966 5 років тому +6

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

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +1

      Hahah I hope that's a good thing?!!?

    • @khurshidallam3966
      @khurshidallam3966 5 років тому +1

      @@KevinNaughtonJr haha. It's! Feels like we are solving some high profile crime case with you.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +1

      @@khurshidallam3966 hahahah in that case let's keep solving these crimes!!!! :)

  • @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 Рік тому

      hey david how are you doing now, what level of coder are you now...?

  • @kushalpatil
    @kushalpatil 2 роки тому

    the return type is int then how it's returning int array??

  • @aikeber1984
    @aikeber1984 3 роки тому +1

    Walmart lab is actually pretty famous in tech world

  • @omerturkakin262
    @omerturkakin262 4 роки тому

    Did you see that you are just returning the array index numbers 1 and 2 . It is like a jog

  • @johnsoto7112
    @johnsoto7112 2 роки тому +1

    how does index return a list?

  • @TrenBlack
    @TrenBlack 4 роки тому +6

    You remind me of Dylan Mckenna

  • @alammahtab08
    @alammahtab08 4 роки тому +2

    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;
    }

  • @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.

  • @mostinho7
    @mostinho7 5 років тому +3

    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)

  • @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.

  • @z41n70
    @z41n70 3 роки тому +1

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

    • @arturonevarez792
      @arturonevarez792 2 місяці тому

      It says return k, k represents the index I guess

  • @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

  • @SaulLee66
    @SaulLee66 3 роки тому +1

    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.

  • @rajatsemwal1865
    @rajatsemwal1865 3 роки тому +1

    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  3 роки тому

      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 3 роки тому

      @@KevinNaughtonJr That would be awesome. but can you solve my this query here please?

    • @KevinNaughtonJr
      @KevinNaughtonJr  3 роки тому +1

      Rajat Semwal the index variable tells you where to place the next unique item

    • @rajatsemwal1865
      @rajatsemwal1865 3 роки тому

      @@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?

    • @Winter_Wyvern1
      @Winter_Wyvern1 Рік тому

      @@rajatsemwal1865 he already answered u for free, and u kept asking follow-up questions without paying;

  • @vk1618
    @vk1618 4 роки тому

    Couldn't think of a good solution

  • @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 :)

  • @arunsagarsa3613
    @arunsagarsa3613 2 роки тому

    Thank you. If possible open source contribution video maybe…

  • @deletrious
    @deletrious 2 роки тому

    no way this is the third most asked question at facebook

  • @bhattyash15
    @bhattyash15 2 роки тому

    what is the time and space complexity here for this approach?

  • @karan150191
    @karan150191 3 роки тому +1

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

    • @arnavraj3415
      @arnavraj3415 3 роки тому

      any fix for that ?

    • @arturonevarez792
      @arturonevarez792 2 місяці тому

      You probably typo in the for condition, it's i < nums.length - 1

  • @kubersharma1971
    @kubersharma1971 4 роки тому

    THANKS FOR THAT MUCH EASE

  • @nathsai1658
    @nathsai1658 5 місяців тому

    Umm, why is Walmart a "Random" company?

  • @jatinkumar4410
    @jatinkumar4410 3 роки тому

    Thanks for the help...

  • @amitsaurabh9948
    @amitsaurabh9948 3 роки тому

    This doesnt work on interviewbit

  • @abhishekshrivastav6193
    @abhishekshrivastav6193 3 роки тому +1

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

  • @TheNickcharlie
    @TheNickcharlie 5 років тому +1

    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  5 років тому

      Anytime! Yeah definitely, wanna shoot me an email and we can find a time to talk?

    • @TheNickcharlie
      @TheNickcharlie 5 років тому

      Thanks Kevin! Could you please give me your email?

    • @TheNickcharlie
      @TheNickcharlie 5 років тому

      thanks, just sent you an email! I appreciate it Kevin :)

  • @jaydeee1726
    @jaydeee1726 3 роки тому

    how to store it in a new declared array?

    • @nadaalqahtani2341
      @nadaalqahtani2341 3 роки тому

      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)

  • @rahulbalan
    @rahulbalan 3 роки тому

    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....

  • @eeshanchanpura3356
    @eeshanchanpura3356 2 роки тому

    Runtime error for me

  • @digishreshamwala1766
    @digishreshamwala1766 3 роки тому

    can you solve this using PHP?

    • @Winter_Wyvern1
      @Winter_Wyvern1 Рік тому

      yeah he can use chinese letters to solve it too

  • @Priyanka-lx2xw
    @Priyanka-lx2xw 3 роки тому

    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

  • @fredwu6812
    @fredwu6812 4 роки тому

    Walmart is not random!

    • @R9000S
      @R9000S 4 роки тому

      Yup they pay the highest

  • @aliquewilliams3080
    @aliquewilliams3080 5 років тому +1

    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 4 роки тому +1

      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 4 роки тому

      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 роки тому +1

      @@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.