Another alternative, if you wanted to count numbers of other characters too, is to use one of the java.util.Arrays.sort() methods on the character array alluded to in some of the other comments. Then you could produce counts of every character with a single pass through the array. If you had an array index type of for loop, you wouldn't need to increment a count, you could just remember the index the current character started at and take the difference of the indexes when the character next changes.