programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

string problem

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

Author and ninkuma
Marshal
Posts: 66806
168
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???

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

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

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.

lowercase baba
Bartender
Posts: 12627
50
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.

Sheriff
Posts: 21459
96

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.

Marshal
Posts: 60130
188
You can even do arithmetic on the chars, eg 'a' - 'a' to get the index of the array.

Gamini Sirisena
Ranch Hand
Posts: 378

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.