Find the first unique character in a string Implementation for a relatively basic programming challenge: 


  Find the first unique character in a string


So evaluating the string ABCA would return B.

My implementation does the following:


Initialize a boolean array that is the size of the input string - note that the default initialization value is false
The boolean array's values correspond to whether or not the input string character that shares the same index value is unique or not.
Compare each character in the input string against all the characters to the right of it
If there is a match, set the values in the boolean array to true for each character index value.
Iterate through the boolean array and return the first value that is false.
If iteration completes, throw a NoUniqueCharacters Exception


Is there a better approach?


I briefly considered using some sort of Ordered Map (if such a thing exists) that maps characters to their counts where the character key values are ordered by their index in the string


public Character identifyFirstUniqueCharacterInString(final String string) throws NoUniqueCharactersException {
        final char[] chars = string.toCharArray();
        final boolean[] repeatedCharacterIndices = new boolean[chars.length];
        for (int candidateCharacterIndex = 0; candidateCharacterIndex &lt; chars.length; candidateCharacterIndex++) {
            for (int comparisonCharacterIndex = candidateCharacterIndex + 1; comparisonCharacterIndex &lt; chars.length; comparisonCharacterIndex++) {
                if (chars[candidateCharacterIndex] == chars[comparisonCharacterIndex]) {
                    repeatedCharacterIndices[candidateCharacterIndex] = true;
                    repeatedCharacterIndices[comparisonCharacterIndex] = true;
                }
            }
        }
        for (int repeatedCharacterIndex = 0; repeatedCharacterIndex &lt; repeatedCharacterIndices.length; repeatedCharacterIndex++) {
            if (!repeatedCharacterIndices[repeatedCharacterIndex]) {
                return chars[repeatedCharacterIndex];
            }
        }

        throw new NoUniqueCharactersException();
    }
One alternative is to let String.indexOf(int, int) do the iteration for you:

return IntStream.range(0, input.length())
                .filter(i -&gt; input.indexOf(input.charAt(i), i + 1) == -1)
                .mapToObj(i -&gt; Character.valueOf(input.charAt(i)))
                .findFirst().orElseThrow(NoUniqueCharactersException::new);



Loop with IntStream from 0 (inclusive) to the length of input (exclusive).
Filter for indices i where input.indexOf(input.charAt(i), i + 1) gives -1, i.e. there is no occurrence of the character at index i past that position.
For any matches, convert to our desired Character wrapper class using mapToObj().
In order to return the first character, we findFirst() from the IntStream, orElseThrow() our custom NoUniqueCharactersException.


A second alternative is to use String.lastIndexOf(int):

// instead of this
// .filter(i -&gt; input.indexOf(input.charAt(i), i + 1) == -1)
// use this
.filter(i -&gt; i == input.lastIndexOf(input.charAt(i)))


In this case, if the last index of the character at position i is indeed i, that means we have found our unique character.