# 【题目】

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

```Given 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 returned length.```

Example 2:

```Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.```

# 【实现】

```class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length < 1) return 0;
int n = nums.length;
int j = 0;
for (int i = 0; i < n; i++) {
if (nums[j] != nums[i]) {
nums[++j] = nums[i];
}
}
return j + 1;
}
}```

0 条评论

• ### Leetcode 26. Remove Duplicates from Sorted Array

版权声明：博客文章都是作者辛苦整理的，转载请注明出处，谢谢！ https://blog.csdn....

• ### Leetcode 26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element app...

• ### leetcode: 26. Remove Duplicates from Sorted Array

leetcode: 80. Remove Duplicates from Sorted Array II

• ### leetcode 26 Remove Duplicates from Sorted Array

Remove Duplicates from Sorted ArrayTotal Accepted: 66627 Total Submissions: 2127...

• ### 【Leet Code】26. Remove Duplicates from Sorted Array

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。

• ### LeetCode笔记：26. Remove Duplicates from Sorted Array

这道题和LeetCode笔记：27. Remove Element这个很类似，只不过这道题的难度在于，在操作中不能随意改变数组中原本元素的位置，因为你要保持它后...

• ### 【LeetCode每日一题】26. Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element...

• ### 关小刷刷题08 – Leetcode 26. Remove Duplicates from Sorted Array 方法2、3

关小刷刷题08 – Leetcode 26. Remove Duplicates from Sorted Array 方法2、3 方法2 方法2：遍历数组，遇到...

• ### 关关的刷题日记07——Leetcode 26. Remove Duplicates from Sorted Array 方法1

关小刷刷题07 – Leetcode 26. Remove Duplicates from Sorted Array 方法1 题目 Given a sorted...

• ### LeetCode-26.Remove Duplicates from Sorted Array | 删除排序数组中的重复项

Given a sorted array nums, remove the duplicates in-place such that each element...

• ### Remove Duplicates from Sorted Array

问题：将有序的数组中重复的数字去掉 分析：由于有序所以只用和前一个比较就行 class Solution { public: int removeDup...

• ### Q26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appe...

• ### Remove Duplicates from Sorted Array II

问题：消除数组中重复次数超过三次的多余的数 分析：若ai-1==ai-2若ai也相等，则清楚ai class Solution { public: in...

• ### Leetcode: Remove Duplicates from Sorted Array

题目： Given a sorted array, remove the duplicates in place such that each eleme...