Skip to content

Instantly share code, notes, and snippets.

@sagarsumit03
Last active December 31, 2018 11:06
Show Gist options
  • Select an option

  • Save sagarsumit03/2f1f3128f287e08242747837f6fe6aea to your computer and use it in GitHub Desktop.

Select an option

Save sagarsumit03/2f1f3128f287e08242747837f6fe6aea to your computer and use it in GitHub Desktop.
class Solution {
public int removeDuplicates(int[] nums) {
//if the list is empty return 1.
if(nums.length == 0) return 0;
//Fast Pointer.
int j = 0;
//to maintain the length.
int count = 1;
//Looping through the array:
for(int i = 0; i < nums.length; i++)
{
//If the entries are not same then copy whats in the ith index to jth index and increment j.
//Example: 1,1,2 now as i keeps increasing nums[i] becomes 2 so, we copy 2 to the j++ index means the second 1.
if(nums[i]!=nums[j])
{
j++;
count++;
nums[j] = nums[i];
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment