In Java, we can find the highest frequency character in a string using either ASCII values or Maps from the Collection framework. If the input string is “all”, ‘l’ is the highest occurred character in the input string “all”.
In this article we will learn how to find highest frequency in a string through example, detailed logic and program explanation for better understanding.
Problem Statement
We need to write a Java program to find the highest frequency character in a string. The program should take input string and should identify the highest repeated character (In alphabetical order) and print the highest occurred character. Let’s see an example,
Example:
For a given input String str “See”, Output Should be “e”.
Explanation:
Input String str: See
Character frequencies:
'S': 1
'e': 2
Repeated character: 'e' (appears twice)
Non-repeated character: 'S' (appears 1 time)
Therefore, "e" is the character with the highest frequency in the input text "See"
Logic to find highest frequency character in a string
There are multiple logics to find the highest frequency character in a string in Java programming language.
Logic 1: Using ASCII value
To find highest frequency character in a String using ASCII value in Java:
- To achieve highest frequency of a character in a string, we will create a method which takes a string as input and remove all the white spaces and convert string to lowercase.
- Initialize an array with length 256 to store the frequency count of each ASCII character.
- Using for loop, it iterates through the characters of the string and updates the corresponding count in the countArray.
- Initializes two variables to store the character with the highest frequency and to store the maximum frequency found so far respectively.
- The program iterates through the countArray to find the character with the highest frequency using for loop:
- Using if-else, it compares the current character in the array with maxFrequency.
- If a character’s frequency is greater than maxFrequency, it updates maxFrequency and maxFrequencyChar. And main method which is entry point for executing the program calls this method to display the result.
Logic 2: Using Maps
To find highest frequency character in a String using Maps in Java:
- To count the highest frequency of a character in a string we will create a method which takes a string as input and remove all the white spaces and convert string to lowercase.
- Creates a HashMap to store character-frequency pairs.
- It iterates through each character in the input string and updates the count of each character in the map using for loop.
- Initializes two variables to store the character with the highest frequency and to store the maximum frequency found so far respectively.
- Using for loop, iterate through Map, Inside the loop, comparing frequencies to find the character with the highest frequency, updating two variables accordingly using if-else. Returns the character with the highest frequency found in the input string. And call this method in the main and get the result.
Java program to find highest frequency character in a string
Program 1: Using ASCII value
In the below program we are using ASCII value to find highest frequency character in a string in Java.
import java.util.Scanner;
public class HighestOccurredChar2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a text");
String str = sc.nextLine();
char highestFrequencyChar = highestOccurredChar(str);
System.out.println("Highest Occurred character: " + highestFrequencyChar);
sc.close();
}
public static char highestOccurredChar(String string) {
string =string.replaceAll("\\s+", "").toLowerCase();
int[] countArray = new int[256];
for (char ch: string.toCharArray()) {
countArray[ch]++;
}
char maxFrequencyChar = '\0';
int maxFrequency = 0;
for (int i = 0; i < countArray.length; i++) {
if (countArray[i] > maxFrequency) {
maxFrequency = countArray[i];
maxFrequencyChar = (char) i;
}
}
return maxFrequencyChar;
}
}
Output:
Enter a text
Occurred Character-Highest
Highest Occurred character: c
Java program explanation to find highest frequency character in a string
- The program takes a text input from the user.
- It removes all whitespace characters from the input string and converts it to lowercase.
- Program creates an integer array
countArray
of size 256 to store character frequencies. - It iterates through the characters of the input string and updates the frequency count in
countArray
. - After counting the frequencies, it iterates through
countArray
to find the character with the highest frequency. - It returns the character with the highest frequency found in the input string.
Program 2: Using Maps
In the below program we are using Maps to find highest frequency character in a string in Java.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HighestOccurredChar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a text");
String str = sc.nextLine();
char highestFrequencyChar = highestRepeatedChar(str);
System.out.println("Highest repeated character: " + highestFrequencyChar);
sc.close();
}
public static char highestRepeatedChar(String string) {
string = string.replaceAll("\\s+", "").toUpperCase();
Map<Character, Integer> countMap = new HashMap<>();
for (char character : string.toCharArray()) {
countMap.put(character, countMap.getOrDefault(character, 0) + 1);
}
int maxFrequency = 0;
char maxFrequencyChar = '\0';
for (Map.Entry<Character, Integer> entry : countMap.entrySet()) {
if (entry.getValue() > maxFrequency) {
maxFrequency = entry.getValue();
maxFrequencyChar = entry.getKey();
}
}
return maxFrequencyChar;
}
}
Output:
Enter a text
Hello, Happy Learning
Highest repeated character: L
Java program explanation to find highest frequency character in a string
- The program asks the user to enter a text input.
- It removes all whitespace characters from the input string and converts it to uppercase.
- Using a HashMap, it counts the frequency of each character in the string.
- It iterates through the characters of the string and updates the frequency count in the map.
- After counting the frequencies, it iterates through the map entries to find the character with the highest frequency.
- It returns the character with the highest frequency found in the input string.
Conclusion
In this article, we have learnt how to find highest frequency character in a string in Java. We have seen the logic and implemented the program using ASCII values and Maps in Java. Hope this article helped you in the understanding of the program.
You can also check our other Java tutorials
- Java Program to find reverse of a String
- Print sum of digits of a number in Java
- Java program to check prime number
- Java program to find maximum among two using switch
Happy Java Programming!!