6
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ This would be cheating, but did you try if the online judge actually checks if the array is manipulated in-place? \$\endgroup\$ – Graipher Mar 28 at 11:44
8
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ @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. \$\endgroup\$ – Graipher Mar 28 at 15:29
  • \$\begingroup\$ @Aethenosity Feel free to post another answer if you see a way to reduce the memory used, though! \$\endgroup\$ – Graipher Mar 28 at 15:30
2
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.