|
| 1 | +<h2><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">26. Remove Duplicates from Sorted Array</a></h2><h3>Easy</h3><hr><div><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>. Then return <em>the number of unique elements in </em><code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>Consider the number of unique elements of <code>nums</code> to be <code>k</code>, to get accepted, you need to do the following things:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Change the array <code>nums</code> such that the first <code>k</code> elements of <code>nums</code> contain the unique elements in the order they were present in <code>nums</code> initially. The remaining elements of <code>nums</code> are not important as well as the size of <code>nums</code>.</li> |
| 7 | + <li>Return <code>k</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p><strong>Custom Judge:</strong></p> |
| 11 | + |
| 12 | +<p>The judge will test your solution with the following code:</p> |
| 13 | + |
| 14 | +<pre>int[] nums = [...]; // Input array |
| 15 | +int[] expectedNums = [...]; // The expected answer with correct length |
| 16 | + |
| 17 | +int k = removeDuplicates(nums); // Calls your implementation |
| 18 | + |
| 19 | +assert k == expectedNums.length; |
| 20 | +for (int i = 0; i < k; i++) { |
| 21 | + assert nums[i] == expectedNums[i]; |
| 22 | +} |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p>If all assertions pass, then your solution will be <strong>accepted</strong>.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong class="example">Example 1:</strong></p> |
| 29 | + |
| 30 | +<pre><strong>Input:</strong> nums = [1,1,2] |
| 31 | +<strong>Output:</strong> 2, nums = [1,2,_] |
| 32 | +<strong>Explanation:</strong> Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. |
| 33 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | + |
| 38 | +<pre><strong>Input:</strong> nums = [0,0,1,1,1,2,2,3,3,4] |
| 39 | +<strong>Output:</strong> 5, nums = [0,1,2,3,4,_,_,_,_,_] |
| 40 | +<strong>Explanation:</strong> Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. |
| 41 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> |
| 49 | + <li><code>-100 <= nums[i] <= 100</code></li> |
| 50 | + <li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li> |
| 51 | +</ul> |
| 52 | +</div> |
0 commit comments