# [LeetCode26] Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

Subscribe to see which companies asked this question

C++实现:

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

#### LeetCode --- 26. Remove Duplicates from Sorted Array

2015-02-02 11:52:37

#### LeetCode-26 Remove Duplicates from Sorted Array(水题-去相同元素)

2015-04-29 17:29:39

#### LeetCode 26 — Remove Duplicates from Sorted Array（C++ Java Python）

2014-02-23 15:12:20

#### Remove Duplicates from Sorted Array II -- LeetCode

2014-04-23 08:39:29

#### Remove Duplicates from Sorted Array -- LeetCode

2014-02-27 05:12:22

#### Remove Duplicates from Sorted Array [python解]

2017-07-24 19:18:55

#### 26. Remove Duplicates from Sorted Array (python)

2016-10-30 15:58:23

#### [leetcode-26]Remove Duplicates from Sorted Array(C)

2015-07-28 12:36:07

#### Leetcode_26_Remove Duplicates from Sorted Array

2014-11-27 23:21:40

#### LeetCode:Remove Duplicates from Sorted Array

2013-09-02 20:42:03