---
# System prepended metadata

title: Question 26. Remove Duplicates from Sorted Array
tags: [leetcode]

---

###### tags: `leetcode`
# Question 26. Remove Duplicates from Sorted Array
### Description:
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
### Solution:
```
Linklist
```
### AC code
code1: rease the duplicate one.
```cpp=class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.size()==0 || nums.size()==1)
            return nums.size();
        auto i = nums.begin();
        int cur = *i;i++;
        while(i!=nums.end()){
            while(i != nums.end() && cur == *i){
                nums.erase(i);
            }
            cur = *i;
            if(i==nums.end())
                break;
            else
                i++;
        }
        return nums.size();
    }
};
```
code2: Just replace the non-duplicate one
```cpp=
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int cnt = 0;
        for(int i = 1 ; i < nums.size() ; i++){
            if(nums[i]==nums[i-1]){
                cnt++;
            }
            else{
                nums[i-cnt] = nums[i];
            }
        }
        return nums.size()-cnt;
    }
};
```