We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 60dab27 + 9a9c665 commit a6fb3ecCopy full SHA for a6fb3ec
rust/0080-remove-duplicates-from-sorted-array-ii.rs
@@ -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