Py solution works on other interpreters but fails on interview bit for [0] test case


#1

Here is my O(n) time complexity and O(1) space complexity solution.

    def removeDuplicates(self, A):
    if len(A) < 1:
        return None
        
    seed = A[0]
    c = 1
    
    for i in range(len(A)):
        if A[i] != seed:
            A[c] = A[i]
            seed = A[i]
            c += 1
            
            
    return A[:c]

The following is the error i’m getting

Traceback (most recent call last):
File “main.py”, line 172, in
cnt -= 1
TypeError: unsupported operand type(s) for -=: ‘list’ and ‘int’

I have not used any variable “cnt”, it error from their side I guess. If anybody knows how to solve it, it would be appreciated. Thankyou!


#2

You need to return the length of the array instead of the array itself.

class Solution:

    # @param A : list of integers

    # @return an integer

    def removeDuplicates(self, A):

        if len(A) <= 1:

            return len(A)

            

        seed = A[0]

        c = 1

        

        for i in range(len(A)):

            if A[i] != seed:

                A[c] = A[i]

                seed = A[i]

                c += 1

        while(len(A)!=c):

            A.pop(-1)

        return len(A)