# [LeetCode 80]Remove Duplicates from Sorted Array II

What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

Subscribe to see which companies asked this question

C++实现: 该版本拓展性较好，将index和i的初始值改成n，index-2改成index-n 即可拓展成保留N位的版本.

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

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

2014-04-23 08:39:29

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

2014-02-23 15:12:20

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

2014-02-27 05:12:22

#### LeetCode(80) Remove Duplicates From Sorted Array II

2015-01-11 03:55:11

#### LeetCode 80. Remove Duplicates from Sorted Array II 解题报告

2016-02-19 13:42:36

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

2016-10-30 15:58:23

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

2014-04-24 08:33:10

#### Remove Duplicates from Sorted List -- LeetCode

2014-04-23 11:25:06

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

2017-07-24 19:18:55

#### [C++]LeetCode 26: Remove Duplicates from Sorted Array（有序数组去重）

2015-05-02 21:43:54