Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Letters Frequency Counter

 
Alfonsus Hartanto
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


Thanks...
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alfonsus Hartanto
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the solution... its cool man...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic