# Leetcode#26 Remove Duplicates from Sorted Array

Difficulty: Easy

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.

int removeDuplicates(vector<int>& nums) {
int len = nums.size();
int a[len],cou=0;
memset(a,0,sizeof(a));
int i = 0;
while(i<len){
if(i>0&&nums[i]==nums[i-1]){
a[i] = 1;
cou++;}
i++;
}
i=0;
while(i<len)
{
if(a[i]==1)
nums[i]=2147483647;
i++;
}
sort(nums.begin(),nums.end());

return len-cou;

}

• 本文已收录于以下专栏：

## LeetCode OJ 26 Remove Duplicates from Sorted Array

• LLL_mj
• 2015年05月25日 14:30
• 383

## LeetCode 26_Remove Duplicates from Sorted Array

• cyfcsd
• 2015年11月25日 15:30
• 339

## LeetCode 26 Remove Duplicates from Sorted Array（从已排序数组中移除重复元素）

• NoMasp
• 2015年11月13日 22:41
• 2253

## leetCode26:Remove Duplicates from Sorted Array

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

## 【leetcode c++】26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicatesin place such that each element appear only once and retu...
• hqq39
• 2015年06月20日 18:46
• 257

## leetcode 26:Remove Duplicates from Sorted Array （C）

举报原因： 您举报文章：Leetcode#26 Remove Duplicates from Sorted Array 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)