Java Program to Find Maximum Occurring Character in a String

Write a Java Program to Find Maximum Occurring Character in a String with an example. First, we assigned -1 as the max value and declared the charFreq integer array of size 256.

The first for loop is to count maxOccStr string characters. The other for loop is to find the maximum occurrence of a character.

import java.util.Scanner;

public class MaxOccurChar1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String maxOccStr;
		char maxChar = ' ';
		int i, max = -1;
		int[] charFreq = new int[256];
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to find Maximum Char Occurrence =  ");
		maxOccStr = sc.nextLine();
		
		for(i = 0; i < maxOccStr.length(); i++)
		{
			charFreq[maxOccStr.charAt(i)]++;
		}
		for(i = 0; i < maxOccStr.length(); i++) 
		{
			char ch = maxOccStr.charAt(i);
			if(max < charFreq[ch]) {
				max = charFreq[ch];
				maxChar = ch;
			}
		}
		System.out.println("\nThe Maximum Occurring Character = " +  maxChar);
		System.out.format("'%c' Character Occurs %d Times ", maxChar, max);	

		
	}
}
Java Program to Find Maximum Occurring Character in a String using for loop

Java Program to Find Maximum Occurring Character in a String using While loop

import java.util.Scanner;

public class MaxOccurChar2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String maxOccStr;
		char maxChar = ' ';
		int i = 0, j = 0, max = -1;
		int[] charFreq = new int[256];
		
		sc= new Scanner(System.in);
		System.out.print("\nEnter String =  ");
		maxOccStr = sc.nextLine();
		
		while(i < maxOccStr.length())
		{
			charFreq[maxOccStr.charAt(i)]++;
			i++;
		}
		while(j < maxOccStr.length()) 
		{
			char ch = maxOccStr.charAt(j);
			if(max < charFreq[ch]) {
				max = charFreq[ch];
				maxChar = ch;
			}
			j++;
		}
		System.out.println("\nThe Maximum Occurring Character = " +  maxChar);
		System.out.format("'%c' Character Occurs %d Times ", maxChar, max);	
		
	}
}

The Maximum Occurring String Character using a While loop output

Enter String =  java programming

The Maximum Occurring Character = a
'a' Character Occurs 3 Times 

It is another code example to find the maximum occurred string character.

import java.util.Scanner;

public class MaxOccurChar3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String maxOccStr;
		int i, max = 0;
		int[] charFreq = new int[256];
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter Any Text =  ");
		maxOccStr = sc.nextLine();
		
		for(i = 0; i < maxOccStr.length(); i++)
		{
			charFreq[maxOccStr.charAt(i)]++;
		}
		for(i = 0; i < 256; i++) 
		{
			if(charFreq[i] > charFreq[max]) {
				max = i;
			}
		}
		System.out.format("\nThe Maximum Occurring Character = %c",  max);
		System.out.format("\n'%c' Character Occurs %d Times ", max, charFreq[max]);	
	}
}
Enter Any Text =  tutorial gateway

The Maximum Occurring Character = a
'a' Character Occurs 3 Times 

This will find the maximum occurring string character example is the same as the above. Here, we separated the logic using Java functions.

import java.util.Scanner;

public class MaxOccurChar4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String maxOccStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String =  ");
		maxOccStr = sc.nextLine();
		
		maxCharOccur(maxOccStr);
	}
	
	public static void maxCharOccur(String maxOccStr) {
		int i, max = 0;
		int[] charFreq = new int[256];
		for(i = 0; i < maxOccStr.length(); i++)
		{
			charFreq[maxOccStr.charAt(i)]++;
		}
		for(i = 0; i < 256; i++) 
		{
			if(charFreq[i] > charFreq[max]) {
				max = i;
			}
		}
		System.out.format("\nThe Maximum Occurring Character = %c",  max);
		System.out.format("\n'%c' Character Occurs %d Times ", max, charFreq[max]);	
	}
}
Enter String =  hello world

The Maximum Occurring Character = l
'l' Character Occurs 3 Times 

Java Program to Return Maximum Occurring Character in a String

First, we declared the charFreq integer array of maxOccStr string length. Next, we used toCharArray and converted the maxOccStr string to the maxOccArr character array. 

The first for loop is used to assign the maxChar character frequency to charFreq array. Then, we assigned the first charFreq character as the maxchar. The other for loop is to find the maximum occurrence in character.

import java.util.Scanner;

public class MaxOccurChar5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String maxOccStr;
		int i, max;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to find Max Char Occur =  ");
		maxOccStr = sc.nextLine();
		
		int[] charFreq = new int[maxOccStr.length()];
		char[] maxOccArr = maxOccStr.toCharArray();
		char maxChar = maxOccStr.charAt(0);
		
		for(i = 0; i < maxOccArr.length; i++)
		{
			charFreq[i] = 1;
			for(int j = i + 1; j < maxOccArr.length; j++) {
				if(maxOccArr[i] == maxOccArr[j] && maxOccArr[i] != ' ' && maxOccArr[i] != '0') {
					charFreq[i]++;
					maxOccArr[j] = '0';
				}
			}		
		}
		max = charFreq[0];
		for(i = 0; i < charFreq.length; i++) 
		{
			if(max < charFreq[i]) {
				max = charFreq[i];
				maxChar = maxOccArr[i];
			}
		}
		System.out.println("\nThe Maximum Occurring Character = " +  maxChar);
		System.out.format("'%c' Character Occurs %d Times ", maxChar, max);			
	}
}
Enter String to find Max Char Occur =  happy coding

The Maximum Occurring Character = p
'p' Character Occurs 2 Times 

Please refer to the following string programs.

  1. Count the Total Occurrence of a Character in a String
  2. Find All Occurrences of a Character in a String
  3. First Character Occurrence in a String
  4. Replace First Character Occurrence in a String
  5. Remove First Character Occurrence in a String
  6. Last Character Occurrence in a String
  7. Replace Last Character Occurrence in a String
  8. Remove the Last Character Occurrence in a String
  9. First and Last Character in a String
  10. Remove The First and Last Character in a String
  11. Minimum Occurring Character in a String
  12. Frequency of each Character in a String
  13. Remove All Occurrences of a Character in a String