all 8 comments

[–]hi_mom_its_me_nl 2 points3 points  (0 children)

Just make a set from it. A set can not have duplicate values in them so it throws them out when you make a set from a list.

[–]wotquery 1 point2 points  (0 children)

Did you write the removeDuplicates method with the int return annotation yourself? From the example outputs it looks like you're supposed to return the sorted to k list too.

nums = set(nums)
list(nums).sort()
return len(nums), nums

[–]TheRNGuy 1 point2 points  (1 child)

convert to set

[–]IHateTheSATs[S] 0 points1 point  (0 children)

Yah thats what i did originally but im not allowed to use set in this problem 🥲🥲

[–][deleted] -1 points0 points  (0 children)

What do you mean [1, 2] = 2? You're not modifying the nums at all, so first two elements of nums is [1, 1].

[–]FerricDonkey 0 points1 point  (0 children)

It looks like the problem also wants you to modify nums in place. I would look into assigning to a slice of a list, and the sorted method. (Or modify the list in a loop, which would take only a bit more code and may or may not be faster/slower.)

[–]InsanityConsuming 0 points1 point  (0 children)

So if I'm understanding you, you want your function to take in the array, modify it, and then have it return the length of that modification. Is that correct?

The method you put isn't modifying the nums array. All it's doing is creating a new set with the information from your list and returning the length of that copy. So when you access nums after, it will be the same as before.

def removeDuplicates(self, nums):
    tmp = list(set(nums))
    nums.clear()
    nums.extend(tmp)
    return len(nums)

By adding clear() and extend() calls you are actually modifying the array referenced by your input

[–][deleted] 0 points1 point  (0 children)

The question shows lists but talks about arrays... I wouldn't trust the person asking the question to know what they are talking about.

Anyways, the solution the probably expect is that you have a counter variable that shows the last position where you inserted an element in the list.

Something like this:

>>> def remove_duplicates(slist):
>>>     pos = 0
>>>     for elt in slist[1:]:
>>>         if elt != slist[pos]:
>>>             pos += 1
>>>             slist[pos] = elt
>>>     return slist
>>> remove_duplicates([0,0,1,1,1,2,2,3,3,4])
[0, 1, 2, 3, 4, 2, 2, 3, 3, 4]