• Post Reply Bookmark Topic Watch Topic
  • New Topic

counting chars in a string  RSS feed

 
Rahul Bharadwaj
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i return that character in a string that appears most often in the string?[ suppose the input is something like "aabcdadbcaefgdhi" the function should return "a" ]

I thought comparing each char with all the others...then storing the count and the corresponding char in a collection as a key value pair. then checking the collection for the maximum count and returning the value.

Is there a shorter/faster way to do this??
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you're using Java 5, be aware that counting characters isn't as easy as it used to be.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't know about shorter or faster, but this seems to work OK

 
Rahul Bharadwaj
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!