|
| 1 | +--- |
| 2 | +id: remove-duplicates-from-sorted-array |
| 3 | +title: Remove Duplicates from Sorted Array (LeetCode) |
| 4 | +sidebar_label: 0026-RemoveDuplicatesFromSortedArray |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | +description: Given a sorted integer array, remove duplicates in-place and return the new length of the array with unique elements. |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Description |
| 12 | + |
| 13 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 14 | +| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | |
| 15 | +| [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Remove Duplicates from Sorted Array Solution on LeetCode](https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | |
| 16 | + |
| 17 | +### Problem Description |
| 18 | + |
| 19 | +Given an integer array `nums` sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in `nums`. |
| 20 | + |
| 21 | +Consider the number of unique elements of `nums` to be `k`, to get accepted, you need to do the following things: |
| 22 | +- Change the array `nums` such that the first `k` elements of `nums` contain the unique elements in the order they were present in `nums` initially. |
| 23 | +- The remaining elements of `nums` are not important as well as the size of `nums`. |
| 24 | +- Return `k`. |
| 25 | + |
| 26 | +### Custom Judge |
| 27 | + |
| 28 | +The judge will test your solution with the following code: |
| 29 | + |
| 30 | +``` |
| 31 | +int[] nums = [...]; // Input array |
| 32 | +int[] expectedNums = [...]; // The expected answer with correct length |
| 33 | +
|
| 34 | +int k = removeDuplicates(nums); // Calls your implementation |
| 35 | +
|
| 36 | +assert k == expectedNums.length; |
| 37 | +for (int i = 0; i < k; i++) { |
| 38 | + assert nums[i] == expectedNums[i]; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +If all assertions pass, then your solution will be accepted. |
| 43 | + |
| 44 | +### Examples |
| 45 | + |
| 46 | +#### Example 1 |
| 47 | + |
| 48 | +- **Input:** `nums = [1,1,2]` |
| 49 | +- **Output:** `2, nums = [1,2,_]` |
| 50 | +- **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). |
| 51 | + |
| 52 | +#### Example 2 |
| 53 | + |
| 54 | +- **Input:** `nums = [0,0,1,1,1,2,2,3,3,4]` |
| 55 | +- **Output:** `5, nums = [0,1,2,3,4,_,_,_,_,_]` |
| 56 | +- **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). |
| 57 | + |
| 58 | +### Constraints |
| 59 | + |
| 60 | +- `1 <= nums.length <= 3 * 10^4` |
| 61 | +- `-100 <= nums[i] <= 100` |
| 62 | +- `nums` is sorted in non-decreasing order. |
| 63 | + |
| 64 | +### Approach |
| 65 | + |
| 66 | +To solve the problem, we can use the two-pointer technique. Here is the step-by-step approach: |
| 67 | + |
| 68 | +1. **Initialize Pointers:** |
| 69 | + - Use `uniqueIndex` to track the position to place the next unique element. |
| 70 | + |
| 71 | +2. **Iterate Through the Array:** |
| 72 | + - Traverse the array starting from the second element. |
| 73 | + - If the current element is different from the element at `uniqueIndex`, move `uniqueIndex` forward and update it with the current element. |
| 74 | + |
| 75 | +3. **Return Result:** |
| 76 | + - The number of unique elements is `uniqueIndex + 1`. |
| 77 | + |
| 78 | +### Solution Code |
| 79 | + |
| 80 | +#### Python |
| 81 | + |
| 82 | +``` |
| 83 | +class Solution(object): |
| 84 | + def removeDuplicates(self, nums): |
| 85 | + if len(nums) == 0: |
| 86 | + return 0 |
| 87 | + |
| 88 | + unique_index = 0 # Pointer for placing unique elements |
| 89 | + |
| 90 | + for i in range(1, len(nums)): |
| 91 | + if nums[i] != nums[unique_index]: |
| 92 | + # Found a unique element, place it in the next position |
| 93 | + unique_index += 1 |
| 94 | + nums[unique_index] = nums[i] |
| 95 | + |
| 96 | + # The number of unique elements is one more than the index of the last unique element |
| 97 | + return unique_index + 1 |
| 98 | +``` |
| 99 | + |
| 100 | +#### Java |
| 101 | +``` |
| 102 | +class Solution { |
| 103 | + public int removeDuplicates(int[] nums) { |
| 104 | + if (nums.length == 0) return 0; |
| 105 | + |
| 106 | + int uniqueIndex = 0; // Pointer for placing unique elements |
| 107 | + |
| 108 | + for (int i = 1; i < nums.length; i++) { |
| 109 | + if (nums[i] != nums[uniqueIndex]) { |
| 110 | + // Found a unique element, place it in the next position |
| 111 | + uniqueIndex++; |
| 112 | + nums[uniqueIndex] = nums[i]; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // The number of unique elements is one more than the index of the last unique element |
| 117 | + return uniqueIndex + 1; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +#### C++ |
| 123 | +``` |
| 124 | +class Solution { |
| 125 | +public: |
| 126 | + int removeDuplicates(vector<int>& nums) { |
| 127 | + if (nums.size() == 0) return 0; |
| 128 | + |
| 129 | + int uniqueIndex = 0; // Pointer for placing unique elements |
| 130 | + |
| 131 | + for (int i = 1; i < nums.size(); i++) { |
| 132 | + if (nums[i] != nums[uniqueIndex]) { |
| 133 | + // Found a unique element, place it in the next position |
| 134 | + uniqueIndex++; |
| 135 | + nums[uniqueIndex] = nums[i]; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // The number of unique elements is one more than the index of the last unique element |
| 140 | + return uniqueIndex + 1; |
| 141 | + } |
| 142 | +}; |
| 143 | +``` |
| 144 | + |
| 145 | +### Conclusion |
| 146 | + |
| 147 | +The above solution efficiently removes duplicates from a sorted array in-place. It employs a two-pointer technique to achieve a time complexity of $O(N)$ and a space complexity of $O(1)$. This ensures that the algorithm can handle input sizes up to the upper limit specified in the constraints efficiently, providing a simple yet effective approach to solving the problem of removing duplicates from a sorted array. |
0 commit comments