My java solution, easy to understand


  • 1
    public int removeDuplicates(int[] A) {
        if (A == null || A.length == 0) {
            return 0;
        }
        int count = 0; //counter of number of accepted integer
        for (int i = 0; i < A.length; i++) {
            if (A[i] != A[count]) {
                count++;
                A[count] = A[i];
            }
        }
        return count+1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.