Java: Find the maximum occurring character in a string
43. Most Frequent Character
Write a Java program to find the character in a string that occurs the most frequently.
Sample Solution:
Java Code:
Sample Output:
The given string is: test string Max occurring character in the given string is: t
Flowchart:
Find the character with the most appearances.
Main.java Code:
Pair.java Code:
Strings.java Code:
Sample Output:
Input text: My high school, the Illinois Mathematics and Science Academy, showed me that anything is possible and that you're never too young to think big. At 15, I worked as a computer programmer at the Fermi National Accelerator Laboratory, or Fermilab. After graduating, I attended Stanford for a degree in economics and computer science. HashMap based solution: Execution time: 3438752 ns (3 ms) Character: e Occurrences :29 ASCII codes based solution: Execution time: 215566 ns (0 ms) Character: e Occurrences :29 Java 8, functional-style solution: Execution time: 120723045 ns (120 ms) Character: e Occurrences :29
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to identify the most frequent character in a string and display its frequency.
- Write a Java program to determine the most common alphabetic character in a string while ignoring case.
- Write a Java program to compute and print the frequency of each character, highlighting the maximum one.
- Write a Java program to find the most frequent character in a string and then remove all its occurrences.
Go to:
PREV : List Items Containing Word Letters.
NEXT : Reverse String Recursively
Java Code Editor:
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.