rename
This commit is contained in:
parent
df84d4f18e
commit
a78fac5f21
6 changed files with 25 additions and 0 deletions
25
remove-duplicates-from-sorted-array-26/src/main.rs
Normal file
25
remove-duplicates-from-sorted-array-26/src/main.rs
Normal 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!");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue