Skip to content

Instantly share code, notes, and snippets.

@CodyEngel
Created March 7, 2015 23:21
Show Gist options
  • Save CodyEngel/98f8c5ecb7e3f35871b0 to your computer and use it in GitHub Desktop.
Save CodyEngel/98f8c5ecb7e3f35871b0 to your computer and use it in GitHub Desktop.
Remove duplicate items from array, solution from Elements of Programming Interviews
public static int removeDuplicates(int[] A) {
if (A.length == 0) {
return 0;
}
int writeIndex = 0;
for (int i = 1; i < A.length; ++i) {
if (A[writeIndex] != A[i]) {
A[++writeIndex] = A[i];
}
}
return writeIndex + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment