Created
March 7, 2015 23:21
-
-
Save CodyEngel/98f8c5ecb7e3f35871b0 to your computer and use it in GitHub Desktop.
Remove duplicate items from array, solution from Elements of Programming Interviews
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 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