
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Maximum Occurred Character of a String in Java
A String class can be used to represent character strings; all the string literals in Java programs are implemented as instances of the String Class. In Java, Strings are constants and their values cannot be changed (immutable) once declared.
Printing Maximum Occurred Character
The maximum occurring character of a string refers to the character that appears multiple times (more than any other character in a string) in a string. To count the maximum occurring character of a string, we have the following approaches -
Using an Array
Using an array is one way to count and print the character that occurs the most in a string. To print the Maximum Occurred Character, follow the steps given below -
- Create an array (freq) to hold the frequency of each alphabet.
- Convert the given string to a character array using the toCharArray() method.
- For each occurrence in the character array, increase the frequency of the respective alphabet in the frequency array (freq).
To retrieve the maximum character -
- Initialize a variable named maxcount to 0.
- Compare each value in the frequency (freq) array with maxcount element by element.
- If any is greater than maxcount, assign its value to maxcount.
- At each step, convert the index of the current element into an alphabet and store it in a variable named maxChar.
Finally, the variable maxChar holds the Maximum occurring character in the string.
Example
In the following example, we define a method called maxOccuredChar(), which converts the passed string to a character array using the toCharArray() method and counts and prints the maximum occurred character of a given string "tutorialspoint":
public class MaxOccuredCharacterTest { public static void main(String[] args) { String str = "tutorialspoint"; String result = maxOccuredChar(str); System.out.println("The maximum occurred character of a string '" + str + "' is: " + result); } public static String maxOccuredChar(String str) { if (str == null || str.isEmpty()) { return "String is empty!"; } int[] freq = new int[26]; for (char c : str.toCharArray()) { freq[c - 'a']++; } int maxCount = 0; char maxChar = ' '; for (int i = 0; i < 26; i++) { if (freq[i] > maxCount) { maxCount = freq[i]; maxChar = (char) (i + 'a'); } } return (maxChar + ", Max count: " + maxCount); } }
The above program prints the maximum occurred character and the maximum count:
The maximum occurred character of a string 'tutorialspoint' is: t, Max count: 3
Max Occurring Character of a String Using HashMap
This is another way to count the maximum occurred character of a string using a HashMap. In Java, HashMap is a data structure that uses the Map interface and a hash table for storing key-value pairs. Finding the maximum occurred character string using HashMap is similar to the previous solution except -
- We use the HashMap object to store the frequency of each character.
- We use the getOrDefault() method to update the character count.
Example
The program below uses a HashMap to store "each character" of the string as a key, and its count (frequency) as the value. It uses the getOrDefault() method to retrieve the current count of a character from the current HashMap:
import java.util.HashMap; public class MaxOccurringCharacter { public static void main(String[] args) { String str = "Welcome to Tutorialspoint"; //creating a HashMap HashMap<Character, Integer> map = new HashMap<>(); char maxChar = ' '; int maxCount = 0; for (char ch : str.toCharArray()) { if (ch != ' ') { int count = map.getOrDefault(ch, 0) + 1; map.put(ch, count); if (count > maxCount) { maxCount = count; maxChar = ch; } } } System.out.println("Max occurring character: " + maxChar); System.out.println("Number of occurrences: " + maxCount); } }
The above program produces the following output:
Max occurring character: o Number of occurrences: 4