# In place solution to remove duplicates from a sorted list

I am working on the problem removeDuplicatesFromSortedList

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.


My solution and TestCase

class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
"""
#Base Case
if len(nums) < 2: return len(nums)

#iteraton Case
i = 0  #slow-run pointer
for j in range(1, len(nums)):
if nums[j] == nums[i]:
continue
if nums[j] != nums[i]: #capture the result
i += 1
nums[i] = nums[j] #in place overriden
return i + 1

class MyCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_raw1(self):
nums = [1, 1, 2]
check = self.solution.removeDuplicates(nums)

def test_raw2(self):
nums = [0,0,1,1,1,2,2,3,3,4]
check = self.solution.removeDuplicates(nums)

unittest.main()


This runs but I get a report:

Runtime: 72 ms, faster than 49.32% of Python3 online submissions for Remove Duplicates from Sorted Array. Memory Usage: 14.8 MB, less than 5.43% of Python3 online submissions for Remove Duplicates from Sorted Array.

Less than 5.43%, I employ the in-place strategies but get such a low rank, how could improve it?

• This would be cheating, but did you try if the online judge actually checks if the array is manipulated in-place? Mar 28, 2019 at 11:44
• You don't test whether the changes are in-place Aug 23, 2020 at 12:01

One way that might speed up your solution slightly is to only place the value if necessary. Also note that only one of the two if conditions can be true, so just use else. Or even better, since there was a continue in the other case, just don't indent it.

i = 0  #slow-run pointer
for j in range(1, len(nums)):
if nums[j] == nums[i]:
continue
# capture the result
i += 1
if i != j:
nums[i] = nums[j] #in place overriden


You can also save half of the index lookups by iterating over the values. Of course it will need slightly more memory this way.

def removeDuplicates(nums):
if len(nums) < 2:
return len(nums)
i = 0  #slow-run pointer
for j, value in enumerate(nums):
if value == nums[i]:
continue
# capture the result
i += 1
if i != j:
nums[i] = value # in place overriden
return i + 1


This can probably be further sped-up by saving nums[i] in a variable as well.

What is interesting is to see timing comparisons to using the itertools recipe unique_justseen (which I would recommend you use in production if you want to get duplicate free values in vanilla Python):

from itertools import groupby
from operator import itemgetter

def unique_justseen(iterable, key=None):
"List unique elements, preserving order. Remember only the element just seen."
# unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
# unique_justseen('ABBCcAD', str.lower) --> A B C A D
return map(next, map(itemgetter(1), groupby(iterable, key)))

def remove_duplicates(nums):
for i, value in enumerate(unique_justseen(nums)):
nums[i] = value
return i + 1


For nums = list(np.random.randint(100, size=10000)) they take almost the same time:

• removeDuplicates took 0.0023s
• remove_duplicates took 0.0026s
• @Aethenosity I read it as both improvements would be welcome (and since they accepted the answer, apparently it was not so far off the mark either). Note that answers are free to comment on any and all aspects of the code here, anyways, regardless of what the OP wants. Mar 28, 2019 at 15:29
• @Aethenosity Feel free to post another answer if you see a way to reduce the memory used, though! Mar 28, 2019 at 15:30
• Your removeDuplicates solution does not work when referring back to resulting list value. input: [1, 1, 2, 3, 3, 3, 3, 5] output: [1, 2, 3, 5, 3, 3, 3, 5] Apr 21, 2020 at 5:10
• @EdwardWilliams That's fine according to the specifications as long as it also returns 4 as the new length. Might have time later to check if this is the case. Apr 21, 2020 at 5:14
• Yeah that is the case, I went and checked :) Apr 21, 2020 at 15:02

A very minor concern: we have this condition:

    if len(nums) < 2: return len(nums)


but none of the included tests exercise it. If we want our testing to be complete, we should have cases with empty and 1-element lists as input.

TBH, I'd reduce that to a simpler condition, and remove the need for one of the tests:

if not nums:
return 0


I have posted this solution and I got 76 ms, faster than 98% on my first submission. But consider this, to check the timer of LeetCode I tried more submissions (with the same exact code) and obtain very different results: 124, 156, 164, 120, 88 ms. While the memory stays always consistent the runtime may vary quite a bit!

    i = 0
for j in range(1, len(nums)):
if (nums[i] != nums[j]):
i += 1
nums[i] = nums[j]
i +=1
return i

• You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. On Code Review, every answer should contain at least a part review.
– Mast
Aug 23, 2020 at 7:26
• It was my first StackOverflow comment so I'm really new to this, what do you mean by a part review? Maybe saying the algorithm works in place and saves memory? It is a really simple algorithm that's why I felt there was no need for comments and such. Aug 27, 2020 at 4:09
• That not the entire answer has to be about the code/approach presented (the review), but every answer must at least do this in part. It's fine if you provide alternative implementations, as long as they're part of a review. Alternative implementations on itself are not a review and not helpful on a site called Code Review.
– Mast
Aug 27, 2020 at 11:34