1

Problem example: Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,,,,,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).


My code:

class Solution(object):
    def removeDuplicates(self, nums):
        count = 0
        array = []
        for i in nums:
            if i not in array:
                array.append(i)
                count+=1
        nums = array
        return count

My count function returns the amount of characters in the final list which is to be expected and my nums array is changed to not have duplicates. yet it still says that my nums is unchanged such as in this example: enter image description here

Although if i run a print function before my return, it outputs the correct nums array as shown here: enter image description here

Colbzyk
  • 29
  • 2

2 Answers2

1

You are not modifying the input list. Change the assignment line

nums = array  
# this just rebinds the local variable nums to a new object!

to

nums[:] = array
# this mutates the actual list object `nums` refers to, 
# the one that was passed to your function

That being said, you don't have to keep a count. You have the length of the final list. Something as short as

class Solution(object):
    def removeDuplicates(self, nums):
        nums[:] = (k for k, _ in groupby(nums))
        return len(nums)

will work since slice assignment nums[:] = ... works with any iterable.

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Alternative alternative: *"That being said, you don't have to build a new list. You have the count"*. That is, `nums[count] = i`. And I'd say that's a better and the intended solution. – Kelly Bundy Jun 26 '23 at 16:52
  • 2
    Your `set` solution is wrong, as it doesn't keep the order. – Kelly Bundy Jun 26 '23 at 16:52
  • You could use `nums[:] = dict.fromkeys(nums)` instead of a set to preserve the order. It's a bit of a cheat though :) – Alain T. Jun 26 '23 at 17:00
  • 1
    @AlainT. Not just a bit. The [problem description](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/) requests it being done [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). – Kelly Bundy Jun 26 '23 at 17:10
  • @AlainT. I had the `dict.fromkeys` solution first, but removed it, because the post made no mention of keeping order. – user2390182 Jun 26 '23 at 18:02
  • 1
    @KellyBundy Are we going to read the leetcode problem or answering the posts as they are? THe OP mentions no keeping order and no strict algorithmic in-place implementation. It also fails to mention that the list is sorted to begin with. – user2390182 Jun 26 '23 at 18:05
  • @user2390182 They wrote *"Remove Duplicates from Sorted Array"*. That's *here*. It's part of the question. So no, they did *not* fail to mention that the list is sorted to begin with. And "remove duplicates" does not suggest *"and do with the others whatever you like"*. And "sorted" should *at least* make one wonder/ask whether the sortedness should be preserved, which to me seems likely, given how easy that is and how useful sortedness is. – Kelly Bundy Jun 26 '23 at 19:18
1

Bearing in mind that the list is guaranteed to be sorted, you only need to test adjacent values.

Something like this:

class Solution:
    def removeDuplicates(self, nums: list[int]) -> int:
        i = 0
        for e in nums[1:]:
            if e != nums[i]:
                i += 1
                nums[i] = e
        return i + 1

nums = [0,0,1,1,1,2,2,3,3,4]

print(Solution().removeDuplicates(nums), nums)

Output:

5 [0, 1, 2, 3, 4, 2, 2, 3, 3, 4]

Note:

Read the brief carefully.

"Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially"

In other words, only the first 5 elements in nums are relevant in this case even though the list retains its original length.

If you only want to print the significant values from the list then:

k = Solution().removeDuplicates(nums)

print(k, nums[:k])

Output:

5 [0, 1, 2, 3, 4]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22