|
| 1 | +[](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript) |
| 2 | +[](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork) |
| 3 | + |
| 4 | +## 26\. Remove Duplicates from Sorted Array |
| 5 | + |
| 6 | +Easy |
| 7 | + |
| 8 | +Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**. |
| 9 | + |
| 10 | +Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements. |
| 11 | + |
| 12 | +Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`. |
| 13 | + |
| 14 | +Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory. |
| 15 | + |
| 16 | +**Custom Judge:** |
| 17 | + |
| 18 | +The judge will test your solution with the following code: |
| 19 | + |
| 20 | + int[] nums = [...]; // Input array |
| 21 | + int[] expectedNums = [...]; // The expected answer with correct length |
| 22 | + |
| 23 | + int k = removeDuplicates(nums); // Calls your implementation |
| 24 | + |
| 25 | + assert k == expectedNums.length; |
| 26 | + for (int i = 0; i < k; i++) { |
| 27 | + assert nums[i] == expectedNums[i]; |
| 28 | + } |
| 29 | + |
| 30 | +If all assertions pass, then your solution will be **accepted**. |
| 31 | + |
| 32 | +**Example 1:** |
| 33 | + |
| 34 | +**Input:** nums = [1,1,2] |
| 35 | + |
| 36 | +**Output:** 2, nums = [1,2,\_] |
| 37 | + |
| 38 | +**Explanation:** 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). |
| 39 | + |
| 40 | +**Example 2:** |
| 41 | + |
| 42 | +**Input:** nums = [0,0,1,1,1,2,2,3,3,4] |
| 43 | + |
| 44 | +**Output:** 5, nums = [0,1,2,3,4,\_,\_,\_,\_,\_] |
| 45 | + |
| 46 | +**Explanation:** 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). |
| 47 | + |
| 48 | +**Constraints:** |
| 49 | + |
| 50 | +* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code> |
| 51 | +* `-100 <= nums[i] <= 100` |
| 52 | +* `nums` is sorted in **non-decreasing** order. |
| 53 | + |
| 54 | +## Solution |
| 55 | + |
| 56 | +```typescript |
| 57 | +function removeDuplicates(nums: number[]): number { |
| 58 | + let n = nums.length |
| 59 | + let i = 0 |
| 60 | + let j = 1 |
| 61 | + if (n <= 1) { |
| 62 | + return n |
| 63 | + } |
| 64 | + while (j <= n - 1) { |
| 65 | + if (nums[i] !== nums[j]) { |
| 66 | + nums[i + 1] = nums[j] |
| 67 | + i++ |
| 68 | + } |
| 69 | + j++ |
| 70 | + } |
| 71 | + return i + 1 |
| 72 | +} |
| 73 | + |
| 74 | +export { removeDuplicates } |
| 75 | +``` |
0 commit comments