Skip to content

Commit a6fb3ec

Browse files
authored
Merge pull request #2318 from raul-sauco/0080-remove-duplicates-from-sorted-array-ii
Create 0080-remove-duplicates-from-sorted-array-ii.rs
2 parents 60dab27 + 9a9c665 commit a6fb3ec

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
// Time O(n) - Space O(1)
3+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
4+
// The maximum number of duplicates allowed.
5+
const MAX_DUPLICATES: usize = 2;
6+
// Nothing to do.
7+
if nums.len() <= MAX_DUPLICATES {
8+
return nums.len() as i32;
9+
}
10+
// A write pointer.
11+
let mut w = MAX_DUPLICATES;
12+
for r in MAX_DUPLICATES..nums.len() {
13+
// Check if the value x positions back is the same.
14+
if nums[r] != nums[w - MAX_DUPLICATES] {
15+
nums[w] = nums[r];
16+
w += 1;
17+
}
18+
}
19+
w as i32
20+
}
21+
}

0 commit comments

Comments
 (0)