Print the character with max occurrence in java.


 package program;
import java.util.Scanner;
public class code
{
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter string in lower case :");
    String s = sc.next();
    int[] frqArr=new int[26];
    for(int i=0; i<s.length(); i++){
        int as=s.charAt(i);
        frqArr[as-97]++;
    }
    int mx=-1;
    for(int i=0; i<frqArr.length; i++){
        if(frqArr[i]>mx){
            mx=frqArr[i];
        }  
    }
    System.out.println("character with max occurrence :");
    for(int i=0; i<frqArr.length; i++){
        if(frqArr[i]==mx)
        System.out.println((char)(i+97));
    }
}
}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.