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