Java solution with only one variable

  • 0

    Basic idea is to use a single cursor to mark the current top of the processed portion, which is also the next vacant place to move next distinct value in.

        if (A.length == 0) return 0;
        int top = 0;
        for (int i = 1; i < A.length; i++) {
            if (A[i] != A[top]) {
                if (top < i) A[top] = A[i]; // move distinct value into vacant cell.
        return top + 1;

  • 0

    i run your code, but there are still duplicates for the array.

Log in to reply

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