Here is a slightly convoluted algorithm that yields the same result as yours but is faster. It takes advantage of Java's native support for arrays instead of using an internal String:
private static char[] getYOURCharArray(char[] array) {
char[] distinctChars = new char[0];
char[] distinctCharsInOriginalOrder = new char[array.length];
for (int i = 0; i < array.length; i++) {
int binarySearchResult = Arrays.binarySearch(distinctChars, array[i]);
if (binarySearchResult < 0) {
char[] updatedDistinctChars = new char[distinctChars.length + 1];
int insertionPointForNewChar = -(binarySearchResult + 1);
System.arraycopy(distinctChars, 0, updatedDistinctChars, 0, insertionPointForNewChar);
updatedDistinctChars[insertionPointForNewChar] = array[i];
System.arraycopy(distinctChars, insertionPointForNewChar, updatedDistinctChars, insertionPointForNewChar + 1, distinctChars.length - insertionPointForNewChar);
distinctChars = updatedDistinctChars;
distinctCharsInOriginalOrder[distinctChars.length - 1] = array[i];
}
}
return Arrays.copyOf(distinctCharsInOriginalOrder, distinctChars.length);
}
Using Arrays.binarySearch(char[], char) to look for a char in a char[] seems to be faster than searching for a char in a String using String.indexOf(int). On the other hand, Arrays.binarySearch(char[], char) requires the char[] to be sorted, which is why we need a second char[] that stores all distinct characters in the order they were first encountered in the original char[], assuming the returned char[] must fulfill this requirement (if it doesn't, then the array distinctCharsInOriginalOrder is actually not needed and you can return distinctChars directly at the end of this method, which might speed up the process a little bit). To ensure that distinctChars stays sorted, it is important to insert new chars at the correct position when updating distinctChars, which is why two calls to System.arraycopy are needed.
I did some simulations with random char arrays, each containing 100000 random characters. Here are the results of a set of 10 simulations:
With String: 9.183 seconds
With char arrays: 2.404 seconds
With String: 4.159 seconds
With char arrays: 2.075 seconds
With String: 4.721 seconds
With char arrays: 2.116 seconds
With String: 4.758 seconds
With char arrays: 2.056 seconds
With String: 4.517 seconds
With char arrays: 2.056 seconds
With String: 4.707 seconds
With char arrays: 2.038 seconds
With String: 4.803 seconds
With char arrays: 2.049 seconds
With String: 4.706 seconds
With char arrays: 2.024 seconds
With String: 4.683 seconds
With char arrays: 2.045 seconds
With String: 4.549 seconds
With char arrays: 2.052 seconds
I have no idea why the first simulation with the String algorithm takes twice as long as all the others. It was like that every time I ran the program, even when I switched the order of the two tests (meaning the first String algorithm still took twice as long as the others, even when the char[] algorithm was tested first). Maybe I did something wrong, or the JVM does something mysterious here.
Apparently, the larger the original char array, the greater the difference between the performance of the two algorithms. Here are the results of 10 simulations with 500000 random characters:
With String: 20.44 seconds
With char arrays: 4.474 seconds
With String: 21.344 seconds
With char arrays: 3.489 seconds
With String: 22.064 seconds
With char arrays: 3.315 seconds
With String: 22.155 seconds
With char arrays: 3.351 seconds
With String: 22.325 seconds
With char arrays: 3.386 seconds
With String: 22.149 seconds
With char arrays: 3.335 seconds
With String: 22.175 seconds
With char arrays: 3.352 seconds
With String: 22.16 seconds
With char arrays: 3.343 seconds
With String: 22.502 seconds
With char arrays: 3.362 seconds
With String: 22.122 seconds
With char arrays: 3.351 seconds