wood burning stoves*
The moose likes Beginning Java and the fly likes string problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string problem" Watch "string problem" New topic
Author

string problem

ayush goel
Greenhorn

Joined: Sep 29, 2008
Posts: 5
how to find the highest occurence of a character in a given string of characters and display that character....
eg: abcdchcggc answer is c
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

What have you written so far?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ayush goel
Greenhorn

Joined: Sep 29, 2008
Posts: 5
sir i think here a counter should b used.
so that we can calculate the maximum occurence of a character but how???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Can we see your code? Its easier to help if we know what you've done already.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Paul, How can we be assured that you are not going to steal ayush's code?
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Originally posted by ayush goel:
how to find the highest occurence of a character in a given string of characters and display that character....
eg: abcdchcggc answer is c


Maybe not the cleanest solution, but a Map of character and number of occurrences. Iterate over all the keys (characters) to know which character has most occurrences.

Another option is to convert into an array or list of characters, sort and then count while iterating.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

a Map might be overkill... since there are only 26 letters in the western alphabet, you could just use an array. iterate through the string, and increment the proper value.

Then, when done parsing the string, loop through the array to find which letter has the most.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

Originally posted by Gamini Sirisena:
Paul, How can we be assured that you are not going to steal ayush's code?

I really hope that was meant as a joke.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Originally posted by fred rosenberger:
a Map might be overkill... since there are only 26 letters in the western alphabet, you could just use an array. iterate through the string, and increment the proper value.

Then, when done parsing the string, loop through the array to find which letter has the most.


Yup. That's a better idea indeed. . Didn't occur to me initially.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
You can even do arithmetic on the chars, eg 'a' - 'a' to get the index of the array.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
 
jQuery in Action, 2nd edition
 
subject: string problem