Win a copy of Practical SVG this week in the HTML/CSS/JavaScript 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...
 
please buy my thing and then I'll have more money:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!