use two index to compress 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||A.length < 1) 6 return 0; 7 if(A.length == 1) 8 return 1; 9 Arrays.sort(A); 10 int slowrunner = 0; 11 int fastrunner = 1; 12 int tmp = 0; 13 while(fastrunner < A.length) 14 { 15 if(A[slowrunner] == A[fastrunner]) 16 { 17 fastrunner++; 18 } 19 else 20 { 21 A[++slowrunner] = A[fastrunner++]; 22 } 23 } 24 if(A[slowrunner] != A[fastrunner-1]) 25 A[++slowrunner] = A[fastrunner-1]; 26 return slowrunner+1; 27 } 28 }