```
int removeDuplicates(int A[], int n) {
if (n < 2) return n;
int pos = 2;
int prev2, prev = A[0];
for (int i = 2; i < n; ++i) {
prev2 = prev;
prev = A[i - 1];
if (prev2 != A[i] || prev != A[i]) {
A[pos++] = A[i];
}
}
return pos;
}
```