Created
January 15, 2016 08:21
-
-
Save jporcelli/540d9aedcaba18149abe to your computer and use it in GitHub Desktop.
Remove Duplicates from Sorted Array II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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