Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active August 29, 2015 14:22
Show Gist options
  • Save cangoal/7656195af94bbb6c0867 to your computer and use it in GitHub Desktop.
Save cangoal/7656195af94bbb6c0867 to your computer and use it in GitHub Desktop.
LeetCode - Remove Duplicates from Sorted Array
//
public int removeDuplicates(int[] nums) {
if(nums == null) return 0;
if(nums.length < 2) return nums.length;
int k = 0, p = 1;
while(p < nums.length){
if(nums[k] != nums[p]){
nums[++k] = nums[p];
}
p++;
}
return k+1;
}
//
public int removeDuplicates(int[] nums) {
if(nums == null) return 0;
if(nums.length < 2) return nums.length;
int k=1;
for(int i=1; i < nums.length; i++){
if(nums[k-1] != nums[i]){
nums[k++] = nums[i];
}
}
return k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment