27. Remove Element Easy

@problem@discussion
#Array#Two Pointers



1/**
2 * [27] Remove Element
3 *
4 * Given an integer array nums and an integer val, remove all occurrences of val in nums <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a>. The relative order of the elements may be changed.
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 val = ...; // Value to remove
13 * int[] expectedNums = [...]; // The expected answer with correct length.
14 *                             // It is sorted with no values equaling val.
15 * int k = removeElement(nums, val); // Calls your implementation
16 * assert k == expectedNums.length;
17 * sort(nums, 0, k); // Sort the first k elements of nums
18 * for (int i = 0; i < actualLength; i++) {
19 *     assert nums[i] == expectedNums[i];
20 * }
21 * 
22 * If all assertions pass, then your solution will be accepted.
23 *  
24 * Example 1:
25 * 
26 * Input: nums = [3,2,2,3], val = 3
27 * Output: 2, nums = [2,2,_,_]
28 * Explanation: Your function should return k = 2, with the first two elements of nums being 2.
29 * It does not matter what you leave beyond the returned k (hence they are underscores).
30 * 
31 * Example 2:
32 * 
33 * Input: nums = [0,1,2,2,3,0,4,2], val = 2
34 * Output: 5, nums = [0,1,4,0,3,_,_,_]
35 * Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
36 * Note that the five elements can be returned in any order.
37 * It does not matter what you leave beyond the returned k (hence they are underscores).
38 * 
39 *  
40 * Constraints:
41 * 
42 * 0 <= nums.length <= 100
43 * 0 <= nums[i] <= 50
44 * 0 <= val <= 100
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/remove-element/
50// discuss: https://leetcode.com/problems/remove-element/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55    pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
56        let (mut i, mut j) = (0, 0);
57        while i < nums.len() {
58            if val != nums[i] {
59                nums[j] = nums[i];
60                j += 1;
61            }
62            i += 1;
63        }
64        j as i32
65    }
66}
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_27() {
76        assert_eq!(Solution::remove_element(&mut vec![3,2,2,3], 3), 2);
77    }
78}
79


Back
© 2025 bowen.ge All Rights Reserved.