How to find characters with the maximum number of times in a string java
Problem statement
In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create a java program that contains the logic to solve this issue.
Solution
Approach - 1 : This problem can be done using hashing technique. You can use the hash map. You need to pass character as key record and integer as value record while creating a hash map. Now the logic is you must store the characters of the given string in the key record. And need to store the count of each character of the string in their respective key-value record. If you encounter the character again, then you need to increment the frequency count of that character in the hash map. You need to traverse the hash map. You need to compare the frequencies of each character and find the maximum frequency. Now store the index of character (say i) at which you got maximum frequency, and finally, you need to print the character at ith index along with its count.
Example 1.
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main (String[] args) {
Scanner sc=new Scanner (System.in);
System.out.println("Please enter the String:");
String repString = sc.next();
FrequencyString StringFreq = new FrequencyString();
StringFreq.Frequency (repString);
}
}
class FrequencyString
{
void Frequency (String s)
{
HashMap<Character,Integer> Freq=new HashMap<>();
for (int i=0;i<s.length();i++)
{
if (Freq.containsKey (s.charAt (i)))
{
Freq.put (s.charAt(i),Freq.get (s.charAt (i))+1);
}
else
{
Freq.put (s.charAt (i),1);
}
}
int max = 0;
int prevMax = 0;
int index = 0;
for (int i=0; i < s.length (); i++)
{
prevMax = Freq.get(s.charAt(i));
if (max <prevMax)
{
max = prevMax;
// storing the new maximum count of a
// character if it exists in the string.
index = i;
// storing the index of maximum repeated character from the String.
}
}
System.out.println (s.charAt (index)+"->"+max);
// printing the max repeated character and its count.
}
}

In the above output, you can see the maximum repeated character from the string. But if the frequency of each character is the same, the above code will output the first character of the string.

Now let us consider the word "Funbun" you can see that 'u' and 'n' are repeated twice. In these cases, you will output the first max repeated character.

Time complexity: O(N)
Space complexity: O(N)
Approach - 2 : This is a brute force approach where you will take an array to store the frequency of each character in the string. You will need to compare every character of the string with each other. Then after you will have to traverse the frequency array along with the character array. When you find the maximum from the frequency array, then you will have to return the character at that index along with its count.
Example 2.
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main ( String [] args) {
Scanner sc = new Scanner ( System.in);
System.out.println("enter the string to get max repeated character");
String repString = sc.next (); // string input
Jtp StringFreq = new Jtp ();
StringFreq.Frequency ( repString );
}
}
class Jtp
{
void Frequency ( String s )
{
int [] freq = new int [s.length()];
char [] str = s.toCharArray();// string to char array
for (int i = 0 ; i < s.length () ; i++)
{
int c = 1;
for (int j = i+1; j < s.length(); j++)
{
if ( str [i] == str[j] ) // comparing each characterof string with other
{
c++;
str [j] = '0';
}
}
freq [i] = c;
}
int max = 0;
int prevMax = 0;
int index = 0;
for (int i = 0; i < freq.length ;i++)
{
prevMax = freq[i];
if (max <prevMax)
{
max = prevMax;
// storing the new maximum count of a
// character if it exists in the string.
index = i;
// storing the index of maximum repeated character from the String.
}
}
System.out.println ( str[index] +" -> " + max); // printing the max repeated chracter with count
}
}
Time complexity: O(N^2)
Space complexity: O(N)


