> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/09-15-2021-no.26-remove-duplicates-from-sorted-array.md).

# 09/15/2021 No.26 Remove Duplicates from Sorted Array

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.

Return `k` *after placing the final result in the first* `k` *slots of* `nums`.

Do **not** allocate extra space for another array. You must do this by **modifying the input array** [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) with O(1) extra memory.

using two pointers: L is for the unrepeated array index, R is for the original array index. Start with L = R = 1; Then check if (nums\[r] != nums\[r -1}, if yes, nums\[L] = nums\[R], L ++;  then R++ every check. Then return L.&#x20;

class Solution {&#x20;

&#x20;     public int removeDuplicates(int\[] nums) {&#x20;

&#x20;           if (nums == null){ return 0; }

```
    int l = 1 ;
    int r = 1;
    int length = nums.length;

    while (l < length && r < length) {// need to remember when <, using nums.length
        if (nums[r] != nums[r - 1] ){
            nums[l] = nums[r];
            l++;
        }

        r++;
    }

    return l;
}
```

}
