I wrote an algorithm to resolve this problem however would love some suggestions on its effectivity and, should you had been an interviewer, what your opinion can be on my resolution. I imagine my time complexity is $O(N)$. Is that this appropriate?
Given a sorted array nums, take away the duplicates in-place such that
every component seem solely as soon as and return the brand new size.
Don’t allocate further house for one more array, it’s essential to do that by
modifying the enter array in-place with O(1) further reminiscence.
Given nums = [0,0,1,1,1,2,2,3,3,4]
Your perform ought to return size = 5, with the primary 5 parts of nums being modified to 0, 1, 2, 3, and four respectively.
It does not matter what values are set within the array past the returned size.
def removeDuplicates(self, nums): """ :sort nums: Listing[int] :rtype: int """ if(nums): IndFirstKind = IndCompare = 0 lengthArray = len(nums) whereas(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