Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
[code]
public class Solution {
public int removeDuplicates(int[] A) {
if( A==null || A.length==0 ) return 0;
if( A.length<3 ) return A.length;
int i=0, j=1, count=1;
for(;j<A.length;j++)
{
if(A[j-1]==A[j])
{
if(count==1)
{
A[++i]=A[j];
count=2;
}
continue;
}
count=1;
A[++i]=A[j];
}
return ++i;
}
}