wood burning stoves
The moose likes Java in General and the fly likes Letters Frequency Counter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Letters Frequency Counter " Watch "Letters Frequency Counter " New topic

Letters Frequency Counter

alfonz jan frithz

Joined: Apr 20, 2009
Posts: 20
I have several solution for this case actually. I just want to ask if this code "sexy" enough.
If you have suggestion, i really appreciate it...

Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Hi Alfonz,

I don't think this algorithm is too "sexy". It's very inefficient. You use a for loop in a for loop which runs through the text again and again and you do other unnecesseary operations.
You can do the counting part with one for loop and a Map. You run through the text with the loop and for every character you have 2 options:
1, Character not in the map, you put it into the map with a value of 1.
2, The Map contains the chraracter: You increment the stored value by one.
From the map you know the:
-number of different characters
-which are these characters
-how many of them you have
So you have all the necesseary information.
I hope it helps.
alfonz jan frithz

Joined: Apr 20, 2009
Posts: 20
thanks for the solution... its cool man...
It is sorta covered in the JavaRanch Style Guide.
subject: Letters Frequency Counter
It's not a secret anymore!