class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= 2) return n;
int pre = 0, cur = 1, count = 1;
while (cur < n) {
if (A[pre] == A[cur] && count == 0) ++cur;
else {
if (A[pre] == A[cur]) --count;
else count = 1;
A[++pre] = A[cur++];
}
}
return pre + 1;
}
};