Your problem is how you have structured the solution.
What you do is traverse the chain in the first loop. In each loop return you go through the entire chain, adding one each time you find the letter of the current character. At the end of each loop of the outer loop you write the current letter with the times you have counted it. This produces the effect you see. For example, you count twice how many 'a' there are, once when you find the first 'a' of 'home', and again when you find the second 'a'.
You have to restructure your solution in the following way:
Initially define a dictionary type variable. This is a complex variable that has keys and values. For each key you can specify a value, and the keys are unique. That is, you can only have one value for a certain key. Let's say it's called contadorLetras
.
You must use a single loop that traverses the chain. In each loop loop, what you do is determine the letter and then use it to access the meter counter, adding one to the value associated with the key of the current letter. Something like this (in pseudocode):
if (contadorLetras[caracter] es nulo) {
contadorLetras[caracter] = 1;
}
else {
contadorLetras[caracter]++; // o contadorLetras[caracter] = contadorLetras[caracter] + 1;
}
That is, when you find the 'c' of 'house', it will make the counterList ['c'] be 1. When finding the first 'a' it will do the same with the 'a', setting the value to 1. With the 's' equal. But when finding the second 'a' what it will do is increase the value of the 'a' in meterLetras.
At the end of the meterLetras loop, it will be valid:
contadorLetras[c] = 1
contadorLetras[a] = 2
contadorLetras[s] = 1
It only remains for you to go through the meterLetters to write the letter (the key) and how many times it has been found (the value).
Since I do not have a Java VM on hand and it's been a long time since I programmed Java, I'll leave the implementation to you. You will have to find the right type for the variable meterLetras, in the comments suggest a Hashmap .