---
# System prepended metadata

title: leetcode 026. Remove Duplicates from Sorted Array
tags: [Leetcode]

---


# leetcode 026. Remove Duplicates from Sorted Array
[leetcode 026. Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) (<font color="#00AF9B"> Easy</font> 通過率: 52.3%)



## 限制條件
<ul>
<li>1 &lt;= nums.length &lt;= 3 * 10^4</li>
<li>-100 &lt;= nums[i] &lt;= 100</li>
<li>nums is sorted in non-decreasing order.</li>
</ul>




### 解法 1
簡單的暴力解，好像也不用解釋啥

- 時間複雜度: O(n)
- 空間複雜度: O(1)

```cpp!=
class Solution {
public:
    int removeDuplicates(vector<int>&amp; nums) {
        int result = 1;
        for(int i = nums.size()-1;i&gt;0;i--)
        {
            if(nums[i] == nums[i-1])
            {
                nums.erase(nums.begin()+i-1);
            }
            else
            {
                result++;
            }
        }
        return result;
    }
};
```
</int>