This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Have you tried any code? If not as a starter you can think about using a Map (letter as the key and the count as the value). Try it and if you have any problem post it with your code. People will help you.
You are now iterating over the string, and for each character iterate over the string again. You can skip the second iteration (the inner loop) by using your Map to store the intermediate results:
Now you can improve the readability a bit if you are using Java 5.0 or higher, using both generics and autoboxing:
Remember that you cannot unbox a null Integer, that's why I have that check there.
Also note that I used Character as keys instead of Strings. That won't improve the code much, but that's the thing you actually want to keep the count for - characters, not strings. [ September 09, 2008: Message edited by: Rob Prime ]
No, count will be boxed regardless. It will use a cached version if between -128 and 127 (inc) yes, but otherwise, it will just create a new Integer object when boxing.
Autoboxing uses Integer.valueOf(int value), which is something like this:
Joined: Sep 08, 2008
I am trying to be smart programmer, now i am lacking of logical skills. Please post logical, technical (cover java concepts) related program type questions for my practice. It will useful to be smart developer.