A string of characters is given.Find the highest occurrence of a character and display that character.

eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE OUTPUT: E or I,J(if equal occurance)


package assignment1;
/*
 * @author snlkjha
 */

public class Charfreq {
   
    public static void charFreq(String x){
       
        String toUpperCase = x.toUpperCase();   /*If the given string is in lower case or mixed case , convert it into upper case.*/
        char y[]=toUpperCase.toCharArray();
        int freq[]=new int[26],max=0;
        for(int i=0;i<y.length;i++) ++freq[(int)y[i]-65];
        System.out.println("________________________________________");
        for(int i=0;i<y.length;i++)
            if(freq[i]!=0)  System.out.println((char)(i+65)+" "+freq[i]);
        for(int i=1;i<freq.length;i++)  max=freq[max]<freq[i]?i:max;
        System.out.println("________________________________________");
        for(int i=0;i<y.length;i++)
          if(freq[i]==freq[max])  System.out.println((char)(i+65)+" "+freq[i]);
        System.out.println("________________________________________");
}
   
    public static void main(String[] args){      
        String x="AAEGBCNAVNEETGUPTAEDAGPE";
        charFreq(x);        
    }
}

INPUT : String x="AAEGBCNAVNEETGUPTAEDAGPE";
OUTPUT:

A 5
B 1
C 1
D 1
E 5
G 3
N 2
P 2
T 2
U 1
V 1
________________________________________
A 5
E 5

INPUT : String x="AAAEGBCNAVNEETGUPTAEDAGPE";
OUTPUT:
________________________________________
A 6
B 1
C 1
D 1
E 5
G 3
N 2
P 2
T 2
U 1
V 1
________________________________________
A 6

Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

Code to upload multiple files simultaneously using JSP, Servlet .

STRING PALINDROME USING STACK AND QUEUE