Programming Examples

Java program to find the Maximum Occurrence character in given string


Write a Java Program to find the Maximum Occurrence character in given string

import java.util.*;
class MaxChar
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any String : ");
        String str=sc.nextLine();
        //str=str.toUpperCase();
        int count=0,max=0;
        char chr=str.charAt(0);
        for(int a=0;a<str.length();a++)
        {
            count=0;
            for(int b=0;b<str.length();b++)
            {
                if(str.charAt(a)==str.charAt(b))
                {
                    count++;
                }
            }
            if(count>=max)
            {
                max=count;
                chr=str.charAt(a);
            }
        }
        System.out.println("Maximum Occurrence Char is : "+chr+" and the number of Occurrence is "+max);
    }
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz