26. Remove Duplicates from Sorted Array Easy
1/**
2 * [26] Remove Duplicates from Sorted Array
3 *
4 * Given an integer array nums sorted in non-decreasing order, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> such that each unique element appears only once. The relative order of the elements should be kept the same.
5 * 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.
6 * Return k after placing the final result in the first k slots of nums.
7 * Do not allocate extra space for another array. You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with O(1) extra memory.
8 * Custom Judge:
9 * The judge will test your solution with the following code:
10 *
11 * int[] nums = [...]; // Input array
12 * int[] expectedNums = [...]; // The expected answer with correct length
13 * int k = removeDuplicates(nums); // Calls your implementation
14 * assert k == expectedNums.length;
15 * for (int i = 0; i < k; i++) {
16 * assert nums[i] == expectedNums[i];
17 * }
18 *
19 * If all assertions pass, then your solution will be accepted.
20 *
21 * Example 1:
22 *
23 * Input: nums = [1,1,2]
24 * Output: 2, nums = [1,2,_]
25 * Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
26 * It does not matter what you leave beyond the returned k (hence they are underscores).
27 *
28 * Example 2:
29 *
30 * Input: nums = [0,0,1,1,1,2,2,3,3,4]
31 * Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
32 * Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
33 * It does not matter what you leave beyond the returned k (hence they are underscores).
34 *
35 *
36 * Constraints:
37 *
38 * 1 <= nums.length <= 3 * 10^4
39 * -100 <= nums[i] <= 100
40 * nums is sorted in non-decreasing order.
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
46// discuss: https://leetcode.com/problems/remove-duplicates-from-sorted-array/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
52 let (mut i, mut j) = (0, 1);
53 while j < nums.len() {
54 if nums[i] == nums[j] {
55 j += 1;
56 } else {
57 i += 1;
58 nums[i] = nums[j];
59 j += 1;
60 }
61
62 }
63 (i + 1) as i32
64 }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_26() {
75 assert_eq!(Solution::remove_duplicates(&mut vec![0,0,1,1,1,2,2,3,3,4]), 5);
76 }
77}
78
Back
© 2025 bowen.ge All Rights Reserved.