> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/wan-quan-an-zhao-jiu-zhang-suan-fa-shua-de-60-dao-zuo-you/ii.-sorted-array/26.-remove-duplicates-from-sorted-array.md).

# 26. Remove Duplicates from Sorted Array

\# Easy

### Solution:

1. consider edge cases: 0 or 1 element
2. use a pointer to track and store current non-repeated element
3. return `length = p+1`

```java
// Java
    public int removeDuplicates(int[] nums) {
        if(nums.length <= 1)
            return nums.length;
            
        int p1 = 0;
        for(int p2 = 1; p2 < nums.length; p2 ++) {
            if(nums[p2] != nums[p1]) {
                p1 ++;
                nums[p1] = nums[p2];
            }
        }
        
        return p1+1;
    }
}
```
