September 2019
Intermediate to advanced
816 pages
18h 47m
English
There are different solutions to finding the first non-repeated character in a string. Mainly, the problem can be solved in a single traversal of the string or in more complete/partial traversals.
In the single traversal approach, we populate an array that's meant to store the indexes of all of the characters that appear exactly once in the string. With this array, simply return the smallest index containing a non-repeated character:
private static final int EXTENDED_ASCII_CODES = 256;...public char firstNonRepeatedCharacter(String str) { int[] flags = new int[EXTENDED_ASCII_CODES]; for (int i = 0; i < flags.length; i++) { flags[i] = -1; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); ...