This commit is contained in:
Håkon Størdal 2025-03-22 13:54:43 +01:00
parent df84d4f18e
commit a78fac5f21
6 changed files with 25 additions and 0 deletions

View file

@ -0,0 +1,25 @@
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
if nums.is_empty() {
return 0;
}
let mut i = 0;
for j in 1..nums.len() {
if nums[j] != nums[i] {
i += 1;
nums[i] = nums[j];
}
}
nums.truncate(i + 1); // Remove extra elements
(i + 1) as i32
}
fn main() {
let mut nums = vec![1, 1, 2];
let new_length = remove_duplicates(&mut nums);
println!("New length: {}", new_length);
println!("Modified nums: {:?}", &nums[..new_length as usize]); // Print only the unique part
println!("Hello, world!");
}