26. Remove Duplicates from Sorted Array Leetcode Python


Given a sorted array, 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 in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
1.countポインタ、indexポインタを定義する
2.繰り返すとindexが増加する
3.繰り返しない場合A[]count=A[index]
1. need two pointers one count another index
2. when duplicated index+=1
when no duplicated A[count]=A[index]
code is as follow
class Solution:
    # @param a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        count=0
        if len(A)<=1:
            return len(A)
        index=1
        count=0
        while index<len(A):
            if A[count]==A[index]:
                index+=1
            else:
                count+=1
                A[count]=A[index]
                index+=1
        return count+1