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 1

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 to find Maximum Char Occurrence =  ");
		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);	
		
	}
}

Java Maximum Occurring String Character using a While loop output

Enter String to find Maximum Char Occurrence =  java programming

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

It is another Java 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 String to find Maximum Char Occurrence =  ");
		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 String to find Maximum Char Occurrence =  tutorial gateway

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

This Java find maximum occurred 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 to find Maximum Char Occurrence =  ");
		maxOccStr = sc.nextLine();
		
		maximumCharOccurence(maxOccStr);
	}
	
	public static void maximumCharOccurence(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 to find Maximum Char Occurrence =  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 a 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 Maximum Char Occurrence =  ");
		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 Maximum Char Occurrence =  happy coding

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