Skip to content

Commit

Permalink
Create remove-duplicates-sorted.java
Browse files Browse the repository at this point in the history
Not an optimal solution, sets solve it much better by just not allowing duplicates.
Use LinkedHashSet next time.
  • Loading branch information
gabedonnan authored Apr 18, 2023
1 parent 8f0d7e8 commit 3fca04f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions remove-duplicates-sorted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int removeDuplicates(int[] nums) {
Set<Integer> map = new HashSet<Integer>();
int shift = 0;
for (int i = 0; i < nums.length; i++) {
if (map.contains(nums[i + shift])) {
for (int j = (i + shift); j < nums.length - 1; j++) {
nums[j] = nums[j+1];
}
shift--;
} else {
map.add(nums[i + shift]);
}
}
return nums.length + shift;
}
}

0 comments on commit 3fca04f

Please sign in to comment.