class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
if not A:
return 0
newTail = 0
for i in range(1, len(A)):
if A[i] != A[newTail]:
newTail += 1
A[newTail] = A[i]
return newTail + 1
Simple Python solution  O(n)

Sorry, I didn't read your code carefully. Now I find out how it works. Can you help me figure out why I got an output limit error? Thx a lot.
class Solution: ''' @param integer[] nums @return integer ''' def removeDuplicates(self, nums): i = 0 while i < (len(nums)  1): if nums[i] is nums[i+1]: del nums[i] else: i += 1 return len(nums)

@yulin_ we are given a sorted array in this problem, and so nums = [1,2,1] is illegal

@AaronWuu note that "Do not allocate extra space for another array, you must do this in place with constant memory", so we should change the element order in nums

@wjymath do you mean when we use function "set", we are actually creating a new array?

@bdeng3 yeah~ notice the doc of set below
class set(object)
 set() > new empty set object
 set(iterable) > new set object


Modified Ur amazing code to remove the duplicated, though not require for the problem
'''
class Solution(object):
def removeDuplicates(self, nums):newTail=0 for i in range(1,len(nums)): if nums[i]!=nums[newTail]: newTail+=1; nums[newTail]=nums[i] for j in range((newTail+1),len(nums)): nums.pop(1) return len(nums),nums
print(Solution().removeDuplicates([1,1,2,3,5,6,6]))
'''

def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() length = len(nums) lastnum = nums[length  1] for i in range(length  2,1,1): currentnum = nums[i] if currentnum == lastnum: nums.remove(currentnum) else: lastnum = currentnum return len(nums)
Runtime Error Message:Line9 : IndexError: list index out of range
Last executed input:[]
Is this a bug? My code (lastnum = nums [length  1]) exceeds the list range?
Can anyone tell me why