66 999
Assignments Done
99,2%
Successfully Done
In November 2018

Answer to Question #14900 in Java | JSP | JSF for Kaze

Question #14900
Write the body of method called maxFrequency:

public static char maxFrequency(String s){

which takes a string (length >=1) as parameter (containing characters from a to z; only small caps), calculates frequencies of characters in the string and returns the character with maximum frequency. For example, if the parameter string is "abcabcabca", the method returns 'a'.
Expert's answer
public class Main {

public static void main(String[] args)
{
//Sample string
String str = "HJDKASDKSHDSHDKJASJDKASJDKJSKDJASKDJKASJDKASJDKLJASDKSDJKSJDKJASKDJSKJDKSADJKLS";
System.out.println(maxFrequency(str));

}
public static char maxFrequency(String s){
int[] counters = new int[28];

//Get the ASCII code of A
int ia = (int)'A';
for (char c : s.toCharArray())
{
int ic = (int)c;
counters[ic - ia]++;
}

int max=counters[0];

for (int i = 0; i < 28; i++){
if(counters[i]>max){
max=counters[i];

}
}
char letter=' ';
for (int i = 0; i < 28; i++){
if (counters[i] > 0 && counters[i]==max)
{
letter=(char)(ia + i);
}
}
return letter;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions