Nav: << previous: 25.K 个一组翻转链表 | next: 27.移除元素 >>


Description

tab: English
 
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears only <strong>once</strong>. The <strong>relative order</strong> of the elements should be kept the <strong>same</strong>.</p>
 
<p>Consider the number of <em>unique elements</em> in&nbsp;<code>nums</code> to be <code>k<strong>​​​​​​​</strong></code>​​​​​​​. <meta charset="UTF-8" />After removing duplicates, return the number of unique elements&nbsp;<code>k</code>.</p>
 
<p><meta charset="UTF-8" />The first&nbsp;<code>k</code>&nbsp;elements of&nbsp;<code>nums</code>&nbsp;should contain the unique numbers in <strong>sorted order</strong>. The remaining elements beyond index&nbsp;<code>k - 1</code>&nbsp;can be ignored.</p>
 
<p><strong>Custom Judge:</strong></p>
 
<p>The judge will test your solution with the following code:</p>
 
<pre>
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
 
int k = removeDuplicates(nums); // Calls your implementation
 
assert k == expectedNums.length;
for (int i = 0; i &lt; k; i++) {
    assert nums[i] == expectedNums[i];
}
</pre>
 
<p>If all assertions pass, then your solution will be <strong>accepted</strong>.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<pre>
<strong>Input:</strong> nums = [1,1,2]
<strong>Output:</strong> 2, nums = [1,2,_]
<strong>Explanation:</strong> Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
</pre>
 
<p><strong class="example">Example 2:</strong></p>
 
<pre>
<strong>Input:</strong> nums = [0,0,1,1,1,2,2,3,3,4]
<strong>Output:</strong> 5, nums = [0,1,2,3,4,_,_,_,_,_]
<strong>Explanation:</strong> Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
	<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image below for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements?



<br>

<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_rem_dup.png" width="500"/>
 
> [!tip]- Hint 2
> 
> We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements.
 
> [!tip]- Hint 3
> 
> Essentially, once an element is encountered, you simply need to <b>bypass</b> its duplicates and move on to the next unique element.
 
 
---
 
[submissions](https://leetcode.com/problems/remove-duplicates-from-sorted-array/submissions/) | [solutions](https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/)
 
 
tab: 中文
 
<p>给你一个 <strong>非严格递增排列</strong> 的数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>只出现一次</strong> ,返回删除后数组的新长度。元素的 <strong>相对顺序</strong> 应该保持 <strong>一致</strong> 。然后返回 <code>nums</code> 中唯一元素的个数。</p>
 
<p>考虑 <code>nums</code> 的唯一元素的数量为 <code>k</code>。去重后,返回唯一元素的数量 <code>k</code>。</p>
 
<p><code>nums</code> 的前 <code>k</code> 个元素应包含 <strong>排序后</strong> 的唯一数字。下标&nbsp;<code>k - 1</code> 之后的剩余元素可以忽略。</p>
 
<p><strong>判题标准:</strong></p>
 
<p>系统会用下面的代码来测试你的题解:</p>
 
<pre>
int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 长度正确的期望答案
 
int k = removeDuplicates(nums); // 调用
 
assert k == expectedNums.length;
for (int i = 0; i &lt; k; i++) {
    assert nums[i] == expectedNums[i];
}</pre>
 
<p>如果所有断言都通过,那么您的题解将被 <strong>通过</strong>。</p>
 
<p>&nbsp;</p>
 
<p><strong class="example">示例 1:</strong></p>
 
<pre>
<strong>输入:</strong>nums = [1,1,2]
<strong>输出:</strong>2, nums = [1,2,_]
<strong>解释:</strong>函数应该返回新的长度 <strong><code>2</code></strong> ,并且原数组 <em>nums </em>的前两个元素被修改为 <strong><code>1</code></strong>, <strong><code>2 </code></strong><code>。</code>不需要考虑数组中超出新长度后面的元素。
</pre>
 
<p><strong class="example">示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>nums = [0,0,1,1,1,2,2,3,3,4]
<strong>输出:</strong>5, nums = [0,1,2,3,4,_,_,_,_,_]
<strong>解释:</strong>函数应该返回新的长度 <strong><code>5</code></strong> , 并且原数组 <em>nums </em>的前五个元素被修改为 <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>2</code></strong>, <strong><code>3</code></strong>, <strong><code>4</code></strong> 。不需要考虑数组中超出新长度后面的元素。
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>-10<font size="1">0</font>&nbsp;&lt;= nums[i] &lt;= 10<font size="1">0</font></code></li>
	<li><code>nums</code> 已按 <strong>非递减</strong>&nbsp;顺序排列。</li>
</ul>
 
 
 
> [!tip]- 提示 1
> 
> In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image below for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements?



<br>

<img src="https://assets.leetcode.com/uploads/2019/10/20/hint_rem_dup.png" width="500"/>
 
> [!tip]- 提示 2
> 
> We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements.
 
> [!tip]- 提示 3
> 
> Essentially, once an element is encountered, you simply need to <b>bypass</b> its duplicates and move on to the next unique element.
 
 
---
 
[提交记录](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/submissions/) | [题解](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122