# 26. Remove Duplicates from Sorted Array

## 1.問題

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

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LRyHSIp5QI9gvyhrjLt%2F-LRyI7Brs0gWMrra1mSM%2F20181123.jpg?alt=media\&token=3e68a29e-2c06-47b1-9efe-6f83b094dc1a)

## 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;
    }
};
```
