public class Solution {
public int removeDuplicates(int[] A) {
if(A.length < 2) {
return A.length;
}
// Scans the array in linear time
int in = 0;
boolean isSec = false;
for(int out = 1; out < A.length; out++) {
if(A[in] != A[out]) {
A[++in] = A[out];
isSec = false;
} else {
if(isSec == false) {
A[++in] = A[out];
isSec = true;
}
}
}
return in + 1;
}
}
Time: O(n)
Space: O(1)