Answer to Question #35702 in Java | JSP | JSF for Amy

Question #35702
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'.
1
Expert's answer
2017-02-24T07:02:24-0500
public static char maxFrequency(String s) {
int length = s.length();
int[] array = new int[26];
int max = Integer.MIN_VALUE;
int index = 0;
for (int i = 0; i < length; i++) {
array[(int) (s.charAt(i) - 97)]++;
}
for (int i = 0; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
index = i;
}
} char c = (char) (index + 97);
return c;

}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS
paypal