Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 int len = A.length; 4 if(len<=2) return len; 5 int count = 1; 6 int p1 =1, p2=1; 7 while(p2<len){ 8 if(A[p2]==A[p2-1]){ 9 if(count<2){ 10 A[p1] = A[p2]; 11 p1++; 12 p2++; 13 count++; 14 } 15 else{ 16 p2++; 17 } 18 } 19 else{ 20 count=1; 21 A[p1] = A[p2]; 22 p1++; 23 p2++; 24 } 25 } 26 return p1; 27 } 28 }