Remove Duplicates from Sorted Array - Leetcode 26 - Python

Ғылым және технология

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Twitter: / neetcode1
Discord: / discord
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
💡 BINARY SEARCH PLAYLIST: • Binary Search
📚 STACK PLAYLIST: • Stack Problems
Problem Link: leetcode.com/problems/remove-...
0:00 - Read the problem
2:12 - Drawing Explanation
8:24 - Coding Explanation
leetcode 26
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#sorted #array #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Пікірлер: 148

  • @NeetCode
    @NeetCode2 жыл бұрын

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

  • @iiju8212

    @iiju8212

    Жыл бұрын

    no more free

  • @saisankalp5889
    @saisankalp58892 жыл бұрын

    Generally we say "non-decreasing order" if the elements in the array are not strictly ascending like: [1,2,3,4,5], "non-decreasing order" means array can have elements like "[1,1,2,2,3]", not strictly ascending. Similar is the case for "non-increasing order".

  • @beyzayildirim0

    @beyzayildirim0

    2 жыл бұрын

    Makes sense now!

  • @predatorgaming895

    @predatorgaming895

    2 жыл бұрын

    💯

  • @scpcomm1215

    @scpcomm1215

    11 ай бұрын

    I'm glad I came here and read the comments.

  • @adilismail3593
    @adilismail35932 жыл бұрын

    Been spending the last 3hours trying to solve this problem. Your explanations are very clear dude. Thanks

  • @ganeshmurmu2338

    @ganeshmurmu2338

    Жыл бұрын

    same here

  • @erniewang399

    @erniewang399

    Жыл бұрын

    yeah this problems confusing, maybe correlates with the many dislikes for this problem

  • @kanishkaakarthigayan8376

    @kanishkaakarthigayan8376

    11 күн бұрын

    I still not able to understand this

  • @paganphoenixguitar
    @paganphoenixguitar2 жыл бұрын

    Awesome videos dude, these have been a super helpful resource as I study for interviews

  • @joshuaduffney
    @joshuaduffney Жыл бұрын

    "Return the number of unique values in the array" is SO much better than saying "Return k", thank you!

  • @symbol767
    @symbol7672 жыл бұрын

    Such a clear explanation and easy to understand solution, thank you man. This was way better explained and coded than the example on Grokking the Coding Interview

  • @vasylynapavelko4036
    @vasylynapavelko4036 Жыл бұрын

    Really like your explanations. I'm using JavaScript, but you make it so easy to undestand the problem and the solution for it.

  • @ahyungrocks5509
    @ahyungrocks55093 ай бұрын

    Was really confused about this problem until watching this video. The solution is so elegant.

  • @AbhishekDesai-lu6ih
    @AbhishekDesai-lu6ihАй бұрын

    Leetcode has the constraint : 1

  • @yanaphatpoonkrasee3409
    @yanaphatpoonkrasee34092 жыл бұрын

    You are awesome. Thank you for all the videos you made. They're really helpful for beginners.

  • @butoyighyslain171
    @butoyighyslain1718 ай бұрын

    did this on my own after checking your other vids, thanks to you Neetcode!

  • @software4live
    @software4live7 ай бұрын

    The array is already sorted. Below is my 1 pointer approach class Solution(object): def removeDuplicates(self, nums): left = 0 while left if nums[left] == nums[left + 1]: del nums[left] else: left +=1 return len(nums)

  • @denysivanov3364

    @denysivanov3364

    3 ай бұрын

    popping element from the middle is 0(n) operation in worst case. You only pop from the end in 0(1) from array (or when you pop and make empty space in the middle, otherwise values need to be copied). Double ended queue has O(N) pop from head and tail.

  • @fiafps

    @fiafps

    3 ай бұрын

    this is O(N^2), make sure youre aware on how costly each array operations is

  • @elizabethr5161
    @elizabethr5161 Жыл бұрын

    Such a clean and brilliant solution.

  • @guptashashwat
    @guptashashwat2 жыл бұрын

    Non-decreasing means all numbers can be equal as well in the array.

  • @erikvandeven100
    @erikvandeven1004 ай бұрын

    Solved it first using a while loop, but using two pointers feels definitely much more easy to understand and explain.

  • @user-he5sr1sk1b
    @user-he5sr1sk1b Жыл бұрын

    2:40 That "say no more!" moment, where i literally stop watching and solve this problem. Understanding is king. Thanks a lot man!

  • @crimsonghoul8983

    @crimsonghoul8983

    3 ай бұрын

    To me, i was confused on how to prevent redundancy. I thought "ok. Let's use 2 pointers and have one start at 0 while the other starts at the next value. Keep the left pointer at one element while the right pointer increments upon every iteration. If both elements are same, then the loop keeps incrementing. If not, swap the values and then increment the left pointer.". I will leave it up to you to see the flaw in this logic.

  • @dennistsai5348
    @dennistsai5348 Жыл бұрын

    You just made it so easy!

  • @vinothiniseenivasan1502
    @vinothiniseenivasan1502 Жыл бұрын

    thanks for good work...crystal clear explanation..

  • @subhamthemusicalguy8851
    @subhamthemusicalguy88512 жыл бұрын

    You are amazing. keep doing the good work

  • @kevinliu9539
    @kevinliu9539 Жыл бұрын

    Thank you! I was a bit confused because I thought you had to null all the other values in the updated array, because the question had _ representing those indices.

  • @Insideoutcest
    @Insideoutcest2 жыл бұрын

    i used a map to track unique values and if i found the number before i spliced it out of the nums array and decremented the loop (to recheck for an indexOf from that position). i didnt use pointers but i had to use more memory.

  • @temuulentem1391
    @temuulentem13912 жыл бұрын

    when i run this it says it works but when i submit it says wrong answer because the output isnt in ascending order.

  • @soumyadas9939
    @soumyadas99392 жыл бұрын

    Thank you, your approach was quite good enough. I just want to know how to think to come upon such a way to solve a problem?

  • @ragerose13
    @ragerose13 Жыл бұрын

    Does anyone have problem running this code with zero or negative value's array on leet code simulation? Whenever i run array that has zero or negative value, the loop left out the very last value.

  • @OmarMohammed44
    @OmarMohammed442 жыл бұрын

    This is so well explained

  • @asiftandel7757
    @asiftandel77572 жыл бұрын

    Great explaination

  • @nathan_milan_blacksmith3926
    @nathan_milan_blacksmith3926 Жыл бұрын

    hi, what drawing app are you using? thanks

  • @generalstep
    @generalstep2 жыл бұрын

    Great Explaination.

  • @ramfattah211
    @ramfattah211 Жыл бұрын

    0:28, because In mathematics, a sequence is said to be in non-decreasing order if each term is greater than or equal to the one before it. In other words, the sequence does not decrease at any point.

  • @Richard-yz2gy
    @Richard-yz2gy3 ай бұрын

    so well explained thankyou

  • @ericametta6964
    @ericametta6964 Жыл бұрын

    The array cant be empty cuz the first constraint in the question is (1

  • @jaredthompkins2472

    @jaredthompkins2472

    Жыл бұрын

    w

  • @FaribaTasniaKhan
    @FaribaTasniaKhan Жыл бұрын

    Oh my God... What an easy solution... And i was thinking all the complex way I can

  • @T_tintin

    @T_tintin

    Жыл бұрын

    I really feel you..

  • @suraj-ej6oq
    @suraj-ej6oq2 жыл бұрын

    Great work 👍👍👍

  • @NeetCode

    @NeetCode

    2 жыл бұрын

    Thanks!

  • @t-distributedkid3825
    @t-distributedkid3825 Жыл бұрын

    Having a third pointer specifically for maintaing the unique value is much easier and more understandable thhan managing with two pointers

  • @kancharajinikar585
    @kancharajinikar58523 күн бұрын

    great explanation

  • @adilsaju
    @adilsaju29 күн бұрын

    this is the best!

  • @AbhishekYadav-vn2xj
    @AbhishekYadav-vn2xj10 ай бұрын

    if array like [0 1 2 2 3] and we are starting from index 1 then r # r+1 so we lose the value 1 cuz 2#1, can someone explain me pls

  • @Lampalot
    @Lampalot Жыл бұрын

    not sure why i get an index out of bound getting the same output using golang

  • @pepelepew4400
    @pepelepew44002 жыл бұрын

    I think a rules-lawyer used non-decreasing because an array 0,0,0,0,0 does not increase/ascend

  • @CEOofTheHood
    @CEOofTheHood2 жыл бұрын

    Hi Mr.Neet can you please cover the convex hull question erect the fence.

  • @fatimadarbandsari1870
    @fatimadarbandsari18702 жыл бұрын

    Thank you

  • @sasidharnaidu4507
    @sasidharnaidu45075 ай бұрын

    Well, I came up with similar approach, using count and last seen element. I actually forgot that this is a classic two pointer problem 😅 which I already had worked on before.

  • @ahmadab9666
    @ahmadab96662 жыл бұрын

    thanks alot

  • @andreytamelo1183
    @andreytamelo11832 жыл бұрын

    Thanks!

  • @NeetCode

    @NeetCode

    2 жыл бұрын

    Thank you so much Andrey!!!

  • @MohamedSalah-xc9nm
    @MohamedSalah-xc9nm4 ай бұрын

    What is wrong when I store these values in a and then print the size of the set?

  • @deluksic

    @deluksic

    3 ай бұрын

    You're using O(k) extra space

  • @sanooosai
    @sanooosai4 ай бұрын

    thank you

  • @williamtempleton7259
    @williamtempleton72592 жыл бұрын

    Hey - love this content. Any ideas why this O(n) solution isn't just as fast as the other solutions, even though they are all pretty much O(n) time ?

  • @venkatasriharsha4227

    @venkatasriharsha4227

    2 жыл бұрын

    It's not about time in this question. It's about extra space.

  • @danteslog5887
    @danteslog588728 күн бұрын

    More optimised approach - int removeDuplicates(std::vector& nums){ int n=nums.size(); int index = 2; if(n

  • @mmmk1414
    @mmmk14142 жыл бұрын

    Thats beautiful and elegant Unlike mine 😂 but it works too

  • @user-kg3kl4sy1w
    @user-kg3kl4sy1w8 ай бұрын

    This is genius! Best video on KZread, thanks bro!

  • @deekshitht786
    @deekshitht786 Жыл бұрын

    No one can replace you❤

  • @tsukuyomi1485
    @tsukuyomi1485 Жыл бұрын

    i put the same codes but its saying theres an error

  • @rddcol
    @rddcol Жыл бұрын

    can someone please tell me what argument is -> int is

  • @rohitkumarjha3406
    @rohitkumarjha3406 Жыл бұрын

    anyone could please send the program that i could execute on my ide with using the same logic

  • @rohitkumarjha3406

    @rohitkumarjha3406

    Жыл бұрын

    please sir help me out i need this logic only as a sokution in my ide

  • @maamounhajnajeeb209
    @maamounhajnajeeb209 Жыл бұрын

    thanks

  • @joelrhine
    @joelrhine11 ай бұрын

    My test cases were accepted but my final submission didn’t get accepted because it exceeded the time limit. That was just a clear reflection of how I code, inefficient but getting the job done. My house is burning but hey, at least we got heat.

  • @KLuqman01
    @KLuqman012 жыл бұрын

    How do you call this code within the IDE. I'm always getting the message ....NameError: name 'List' is not defined

  • @gunahawk6893

    @gunahawk6893

    2 жыл бұрын

    from typing import List

  • @fahadahmedakash8045
    @fahadahmedakash80459 ай бұрын

    why using set gives wrong answer? it prints the correct value in stdout but wrong in output

  • @BurhanAijaz
    @BurhanAijaz4 ай бұрын

    10:04 u said what if we have an empty array , but according to the constraints , the minimum length of input will be 1 so that is why leetcode accepts it: constraint: 1

  • @mohamedelhadi7088
    @mohamedelhadi7088 Жыл бұрын

    I came up with a crafty solution xD, but it's in O(n^2) !

  • @chrislee8319
    @chrislee83193 ай бұрын

    For anyone who is seeing this comment, don't feel ashamed of watching the solution for an easy level leetcode, keep the grit and keep grinding. You'll eventually succeed!

  • @doc9448

    @doc9448

    3 ай бұрын

    At the end of the day, the task is to understand / be able to do what you previously did not understand / were not able to do. Whether you do it yourself or it's a learning experience for you, progress is nevertheless being made. For me, I'm trying to use my limited time efficiently so I'd rather look at the solution and internalize the logic and syntax then spend however much longer trying to solve it for myself (and possibly end up having to spend time learning the solution from scratch anyway).

  • @Famelhaut
    @Famelhaut9 ай бұрын

    This problem is written like shit. This comment helped solve it without watching the solution: "They don't really want you to remove the duplicates. They want you to sort the uniques at the front, then return the length of the sorted part. Then, behind the scenes, they slice the array at the length you give them and the result of that is what they check. Just FYI, this sh_t drove me crazy..."

  • @JohnBalzac

    @JohnBalzac

    7 ай бұрын

    Yep this is the only thing that explains why we are not exactly "removing" duplicates from the array, a comment on LeetCode lol. We return the number they use to splice the array, it was excruciating trying to figure out what I was supposed to do with the rest of the freaking array!

  • @saurabhkumarsrivastava6034
    @saurabhkumarsrivastava6034 Жыл бұрын

    Great man salute 😄nicely explained

  • @atalaykarahan1066
    @atalaykarahan106611 ай бұрын

    i did not understand when i try to use hash set and return the count of hash leetcode return me a error can we solve this problem with hash set? if its not why?

  • @SuubUWU

    @SuubUWU

    4 ай бұрын

    Sorry if it’s too late but it’s because the problem requires you to not allocate extra space. There’s probably an edge case where each element is unique, like all evens [2,4,6,8] Worst case scenario, your hash grew to the space time to O(n). Problem requires an O(1) space time solution.

  • @kanchidoshi6907
    @kanchidoshi69072 жыл бұрын

    Sorry but this code doesn't return the output array with non duplicate values?

  • @abnerramirez6209
    @abnerramirez620910 ай бұрын

    when i put the same code you did I still didn't get the write answer

  • @fuchiahuang3822
    @fuchiahuang38222 жыл бұрын

    Thanks for your clear explaination, but what if the size of nums is 0 or 1?

  • @yonahbyarugaba

    @yonahbyarugaba

    2 жыл бұрын

    We can't have duplicates if size is 0 or 1, so that wouldn't be a valid input I guess

  • @T_tintin
    @T_tintin Жыл бұрын

    I actually spend so much time solving it.. that too by shifting the elements but it could have done a much simpler way. I really don't know if I will ever get good enough to reach the point where the best solution just clicks after some thinking .it's so frustrating

  • @Pratik-tk6ts

    @Pratik-tk6ts

    Жыл бұрын

    You will !

  • @Sabre5106
    @Sabre510610 ай бұрын

    Why is this code not working for the solution? class Solution: def removeDuplicates(self, nums): uniqueset = set() for n in nums: uniqueset.add(n) return len(uniqueset) I've tried this outside of LeetCode's checkers and it returns an integer of the expected length. Doing print(uniqueset) returns a set of the expected values, but Leetcode's checker says it returns a completely different list of values. I'm very confused as to what has gone wrong, but I am new to Python3.

  • @Sabre5106

    @Sabre5106

    10 ай бұрын

    Having watched the start of the video, you say that we can't initialise another array (and presumably a set as I've used) but this is not the state of the problem on the website today 1yr after this video was posted. It simply says to return k. Utterly stupid problem, just gonna skip this one.

  • @goldberg6330

    @goldberg6330

    10 ай бұрын

    @@Sabre5106 u should modify nums inplace according to the problem its more important than returning the length of unique elements. When u submit the code they check both nums and the value u r returning but u are not modifying the nums list

  • @TheCreepyNoob150
    @TheCreepyNoob1502 ай бұрын

    I tried one thing class Solution(object): def removeDuplicates(self, nums): answer=[] answer.append(nums[0]) uniquenums=1 for i in range(len(nums)): if nums[i] not in answer: uniquenums=uniquenums+1 answer.append(nums[i]) return uniquenums I don't know why it's not coming at all. Despite it being logically correct. Can somebody help me with this. When I checked it in the IDE it is coming perfectly, but when it comes to Leetcode it returns 1 as the output. Please help

  • @alanjerryson883

    @alanjerryson883

    2 ай бұрын

    same

  • @mio1201
    @mio12016 ай бұрын

    LeetCode accepts this solution because there is a constraint for len(nums) to be => 1, so there always be at list 1 unique number in nums

  • @jst8922
    @jst89225 ай бұрын

    I don't have lc account, but code from 10:15 doesn't replace duplicates with null/none/blank space/etc... this does: def removeDuplicates(nums): l = 1 for r in range(1, len(nums)): if nums[r] != nums[r-1]: nums[l] = nums[r] l += 1 for i in range(l, len(nums)): nums[i] = None return nums nums = [1, 2, 2, 3, 4, 4, 4, 5, 5] removeDuplicates(nums) print(nums) # output: [1, 2, 3, 4, 5, None, None, None, None]

  • @user-re7jf4tx5g
    @user-re7jf4tx5g6 ай бұрын

    Does this channel not have ads?

  • @WisdomShortvids
    @WisdomShortvids Жыл бұрын

    easy with numbers what about strings ?

  • @amalthiru206
    @amalthiru206 Жыл бұрын

    Why can't we just convert it to a set, and then convert it to a set and then return the length of the set?

  • @eklavya_axioms

    @eklavya_axioms

    Жыл бұрын

    because we should not use extra space in this question. This means we cannot take any other data structure.

  • @hasan_shans
    @hasan_shans2 жыл бұрын

    What if we have [1,2,3,4,4] ? Two values are different, but if we go with this solution, we will end up overriding the unique values.

  • @reisturm9296

    @reisturm9296

    2 жыл бұрын

    The algorithm still works for your case . [1,2,3,4,4] L=1, R=1 First loop, if condition is true, so we place nums[R] at index L, so "replace" 2 with 2. Increment both pointers. Same thing happens for 3 and 4. We finally get to the duplicate 4, we'd leave the L pointer behind and only increment R pointer.

  • @prafulparashar9849

    @prafulparashar9849

    2 жыл бұрын

    @@reisturm9296 Very good catch 👍

  • @T_tintin

    @T_tintin

    Жыл бұрын

    It will work

  • @sumyatnoe7128
    @sumyatnoe712810 ай бұрын

    In my opinion, how about we used sort function? Answer : unique_arr = list(set(arr))

  • @8DMafia

    @8DMafia

    10 ай бұрын

    you need to have 0(1) space answer.

  • @riccimel
    @riccimel8 ай бұрын

    Can we say that it is some way a sliding window?

  • @HSnake5
    @HSnake5 Жыл бұрын

    Problem seems to be very simple, but also seems to be written by an alien

  • @aadityakiran_s
    @aadityakiran_s Жыл бұрын

    About as efficient as it can get huh? But the LeetCode statistics aren't that reliable in terms of efficiency right?

  • @prempeacefulchannel
    @prempeacefulchannel2 жыл бұрын

    Awesome as always🙌🏻

  • @NeetCode

    @NeetCode

    2 жыл бұрын

    Thanks!

  • @srikanthpoojary867
    @srikanthpoojary8672 жыл бұрын

    Thanks

  • @NeetCode

    @NeetCode

    2 жыл бұрын

    Hey Srikanth - thank you so much, I really appreciate it!! 😊

  • @davide816
    @davide816 Жыл бұрын

    i don't understand why we can't use the pop function

  • @dipanjanjayswal554
    @dipanjanjayswal5542 жыл бұрын

    Hey NeetCode Can't we simply do like: k = set(nums) return list(k) The set function automatically removes duplicates. Still it's showing error. Can you please explain why set function isn't working

  • @lucifers6105

    @lucifers6105

    2 жыл бұрын

    I have the same exact problem, Like for real why doesn't it work with a set ??

  • @dipanjanjayswal554

    @dipanjanjayswal554

    2 жыл бұрын

    @@lucifers6105 actually I later realised that set function doesn't perform the operation inplace. Instead it creates an temporary array for the same. 😁

  • @morningstar3996

    @morningstar3996

    2 жыл бұрын

    @@dipanjanjayswal554 I think you are right but still, I managed to find a way to make work with a set implementation. new_nums = set(nums) new_nums = list(new_nums) new_nums.sort() for i in range(len(nums)): try: nums[i] = new_nums[i] except: nums.pop() return len(nums)

  • @dipanjanjayswal554

    @dipanjanjayswal554

    2 жыл бұрын

    @@morningstar3996 good approach dude.. out of the box thought 💭 👍🏻

  • @nishantsharma7596
    @nishantsharma7596 Жыл бұрын

    def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ x = 0 while x val = nums[x] if nums[x+1:].count(val) >= 1: # nums.remove(val) nums[x+1:] = list(filter(lambda a: a != val, nums[x+1:])) else: x += 1 return len(nums) How is this approach?

  • @_XY_
    @_XY_2 жыл бұрын

    You saved my life

  • @Dennis-Ong
    @Dennis-Ong2 жыл бұрын

    This question really confuse me lol..the wording itself is a quiz

  • @Lambyyy

    @Lambyyy

    2 жыл бұрын

    True! The solution is pretty simple, just the instructions are strange lol

  • @palyanytsia
    @palyanytsia5 ай бұрын

    dislikes for this problem on the leetcode are from everyone who tried to create a new array from num. Not "in-place". It did that too, misread this part of requirements 😪

  • @TutorialTechie
    @TutorialTechie Жыл бұрын

    public int removeDuplicates(int[] nums) { int k = 1; // k is the left pointer for (int i = 1; i if (nums[i] != nums[i - 1]) nums[k++] = nums[i]; } return k; }

  • @WaldoTheWombat
    @WaldoTheWombatАй бұрын

    This is my solution, but yours is better: def removeDuplicates(self, nums: List[int]) -> int: l = 0 r = 0 # or 1? while r while r+1 r += 1 nums[l] = nums[r] r += 1 l += 1 return l

  • @midrees1286
    @midrees1286 Жыл бұрын

    def removeDuplicates(self, nums: List[int]) -> int: prev = None j=0 for i in nums: if i != prev: nums[j] = i j+=1 prev = i return j

  • @surbhirohilla5139
    @surbhirohilla51392 жыл бұрын

    why can't we return nums array sir?

  • @sajramkisho9991

    @sajramkisho9991

    2 жыл бұрын

    because the return type is given as int and not int []

  • @muhammaddelen7769
    @muhammaddelen77692 жыл бұрын

    this solution is uncharacteristically confusing for Neetcode. My recommended solution: prevNum = -101 i = 0 while i if nums[i] == prevNum: # repeat nums.pop(i) i -= 1 #keep the loop the same else: #not repeat prevNum = nums[i] i +=1 return len(nums)

  • @prafulparashar9849

    @prafulparashar9849

    2 жыл бұрын

    So, I did the same thing first off when i read the question. But the problem with this is - popping an element from the index (i) takes O(n) complexity. so your solution is having the complexity of O(n^2). On the other hand, the solution in the video shows the approach of 2 pointers and achieves the answer in a single pass. You can try submitting your answer but will see a time inefficiency in the submission but your approach is absolutely correct.

  • @NotNotNithin
    @NotNotNithin Жыл бұрын

    Java solution: public static int removeDuplicates(int[] arr) { int left = 1; for (int right = 1; right if (arr[right] != arr[right - 1]) { arr[left] = arr[right]; left++; } } return left; }

  • @JBEedits
    @JBEedits10 ай бұрын

    It not delete just reassign value

  • @blackplaydoh3522
    @blackplaydoh3522 Жыл бұрын

    I hated this problem

  • @zainio
    @zainio2 жыл бұрын

    Kinda similar to String Compression right?

  • @alanjerryson883
    @alanjerryson8832 ай бұрын

    What is the problem? Everything works fine in my paycharm, but not on the litcode class Solution: def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ tmp = [] for i in nums: if i not in tmp: tmp.append(i) nums = tmp return len(tmp) ------------------------------------------------------------------ Input nums = [0,0,1,1,1,2,2,3,3,4] Output [0,0,1,1,1] Expected [0,1,2,3,4]

  • @mberu14
    @mberu14Ай бұрын

    the assign right to left is silly ,it is pointless is easy if we just element the duplicates and assign just a single digit needs to be explained better

  • @igboman2860
    @igboman28602 жыл бұрын

    def removeDuplicates(self, nums: List[int]) -> int: i=0 for j in range(1,len(nums)): if nums[j] != nums[i]: nums[i+1],nums[j] = nums[j], nums[i+1] i += 1 return i + 1

Келесі