package eecs1022.finala; /** * This question is worth 2.5 marks. No partial marks will be awarded for this question. */ public class Question7 { /** * Returns the number of occurrences of a most occurring character in the given string. * You may assume that the string is not null and not empty. You may may also assume * that all characters are lower case letters. * * @param s a string. * @return the number of occurrences of a most occurring character in the given string. */ public static int mostOccurrences(String s) { int max = 0; for (char c = 'a'; c <= 'z'; c++) { int count = 0; for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (letter == c) { count++; } } if (count > max) { max = count; } } return max; } /** * Tests method. You may add more test cases. * * @param args not applicable. */ public static void main(String[] args) { System.out.println(Question7.mostOccurrences("a")); System.out.println(Question7.mostOccurrences("ab")); System.out.println(Question7.mostOccurrences("abc")); System.out.println(Question7.mostOccurrences("abca")); System.out.println(Question7.mostOccurrences("aaa")); } }