Skip to content

Commit 2bc2fd9

Browse files
authored
Merge pull request neetcode-gh#3038 from benmak11/remove-dups
Create 0080-remove-duplicates-from-sorted-array-ii.java
2 parents 317d899 + b1b81b2 commit 2bc2fd9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

java/0080-remove-duplicates-from-sorted-array-ii.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
int l = 0, r = 0, n = nums.length;
4+
5+
while (r < n) {
6+
int count = 1;
7+
while (r + 1 < n && nums[r] == nums[r + 1]) {
8+
r++;
9+
count++;
10+
}
11+
12+
for (int i = 0; i < Math.min(2, count); i++) {
13+
nums[l] = nums[r];
14+
l++;
15+
}
16+
r++;
17+
}
18+
return l;
19+
}
20+
}
21+
122
/*
223
* Time Complexity: O(n);
324
* Space Complexity: O(1);
@@ -36,3 +57,4 @@ public int removeDuplicates(int[] nums) {
3657
}
3758
}
3859

60+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy