Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int removeDuplicates(int[] nums) {
- // [1, 1, 1, 1, 2, 2, 3, 4, 4, 4]
- // n d d d n d n n d d
- // n – non-duplicate.
- // d – duplicate.
- // We will put non-duplicated numbers at positions
- // 0, 1, 2, 3, 4, ..., etc. one-by-one.
- // Number nums[0] should stay at position 0.
- // We will start filling non-duplicated numbers
- // from position 1.
- int j = 1;
- // Iterate starting from i = 1.
- for (int i = 1; i < nums.length; i++) {
- // If it's a duplicate (d), skip it.
- if (nums[i] == nums[i - 1]) {
- continue;
- }
- // Otherwise (n), put it at index j,
- // and then increment j.
- nums[j++] = nums[i];
- }
- return j;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement