Skip to content

Commit 3fca04f

Browse files
authored
Create remove-duplicates-sorted.java
Not an optimal solution, sets solve it much better by just not allowing duplicates. Use LinkedHashSet next time.
1 parent 8f0d7e8 commit 3fca04f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

remove-duplicates-sorted.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
Set<Integer> map = new HashSet<Integer>();
4+
int shift = 0;
5+
for (int i = 0; i < nums.length; i++) {
6+
if (map.contains(nums[i + shift])) {
7+
for (int j = (i + shift); j < nums.length - 1; j++) {
8+
nums[j] = nums[j+1];
9+
}
10+
shift--;
11+
} else {
12+
map.add(nums[i + shift]);
13+
}
14+
}
15+
return nums.length + shift;
16+
}
17+
}

0 commit comments

Comments
 (0)