# string problem

ayush goel
Greenhorn
Posts: 5
how to find the highest occurence of a character in a given string of characters and display that character....

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65276
95
What have you written so far?

ayush goel
Greenhorn
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
Posts: 10336
Can we see your code? Its easier to help if we know what you've done already.

Gamini Sirisena
Ranch Hand
Posts: 378
Paul, How can we be assured that you are not going to steal ayush's code?

Shashank Agarwal
Ranch Hand
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....

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
Posts: 12231
36
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.

Rob Spoor
Sheriff
Posts: 20708
68
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.

Shashank Agarwal
Ranch Hand
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
Posts: 50687
83
You can even do arithmetic on the chars, eg 'a' - 'a' to get the index of the array.

Gamini Sirisena
Ranch Hand
Posts: 378