similar with Remove Duplicates from Sorted Array
1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null) 6 return 0; 7 if(A.length < 3) 8 return A.length; 9 int slowrunner = 0; 10 int fastrunner = 1; 11 boolean hasDup = false; 12 while(fastrunner < A.length) 13 { 14 if(A[slowrunner] == A[fastrunner]) 15 { 16 if(hasDup) 17 fastrunner++; 18 else{ 19 A[++slowrunner] = A[fastrunner++]; 20 hasDup = true; 21 } 22 } 23 else 24 { 25 A[++slowrunner] = A[fastrunner++]; 26 hasDup = false; 27 } 28 } 29 30 return slowrunner+1; 31 } 32 }