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

# 26. Remove Duplicates from Sorted Array

## 1.問題

* 給予一個sorted array, 移除重複出現的element並回傳新array的長度

![](/files/-LRyI7Brs0gWMrra1mSM)

## 2.想法

* 用一個index maintain已篩選過的array

## **3.程式碼**

```
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.size() == 0) {
            return 0;
        }
        int lastIndex = 1, j = 1;
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i] > nums[i - 1]) {
                nums[lastIndex] = nums[i]; 
                lastIndex = ++j;
            }
        }
        
        return j;
    }
};
```
