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]) -&gt; int:
        """
        """
        #Base Case
        if len(nums) &lt; 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)
        answer = 2
        self.assertEqual(check, answer)

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

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?
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) &lt; 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') --&gt; A B C D A B
    # unique_justseen('ABBCcAD', str.lower) --&gt; 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