Skip to content

Instantly share code, notes, and snippets.

@jporcelli
Created January 15, 2016 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jporcelli/540d9aedcaba18149abe to your computer and use it in GitHub Desktop.
Save jporcelli/540d9aedcaba18149abe to your computer and use it in GitHub Desktop.
Remove Duplicates from Sorted Array II
public class Solution {
public int removeDuplicates(int[] A) {
if(A.length == 1){
return 1;
}
int duplicates = 0, local_duplicates = 1;
for(int i = 0 ; i < A.length - 1;){
if(A[i] == A[i + 1] && i < (A.length - duplicates)){
local_duplicates++;
if(local_duplicates > 2){
shiftOutDuplicate(A, i + 1);
duplicates++;
}else{
i++;
}
}else{
local_duplicates=1;
i++;
}
}
return A.length - duplicates;
}
private void shiftOutDuplicate(int[] A, int i){
A[i] = -999;
for(int j = i; j < A.length - 1; j++){
int temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment