Given a sorted array nums, remove the duplicates in-place I wrote an algorithm to solve this challenge but would love some feedback on its efficiency and, if you were an interviewer, what your opinion would be on my solution. I believe my time complexity is \$O(N)\$. Is this correct?

The Task:


  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:
    Given nums = [0,0,1,1,1,2,2,3,3,4]
    
    Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
    
    It doesn't matter what values are set in the array beyond the returned length.
  


My Solution:

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if(nums):
        IndFirstKind = IndCompare = 0
        lengthArray = len(nums)
        while(IndCompare &lt; lengthArray):
            if nums[IndFirstKind] == nums[IndCompare]:
                IndCompare = IndCompare + 1
            else:
                if(IndFirstKind + 1 != IndCompare):
                    temp = nums[IndCompare]
                    nums[IndCompare] = nums[IndFirstKind + 1]
                    nums[IndFirstKind + 1]= temp
                IndFirstKind = IndFirstKind +1
                IndCompare = IndCompare + 1
        return IndFirstKind + 1
    return 0
This is a job for itertools.groupby:

i = 0
for key, _ in groupby(nums):
    nums[i] = key
    i += 1
return i


A look at the implementation of groupby shows that it allocates \$O(1)\$ extra space, as required by the problem.

The i += 1 could be avoided using enumerate, like this:

i = -1 # in case nums is empty
for i, (key, _) in enumerate(groupby(nums)):
    nums[i] = key
return i + 1


but the need for an exceptional case to handle empty input is rather inelegant.