Home > Software engineering >  Finding the Most Frequent Letter
Finding the Most Frequent Letter

Time:11-12

Input: A string of characters. All letters will be upper case. The String Variable will be defined at the beginning of the program.

String sentence = "IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS";

Output: Answer the following questions about the string:

What is the most frequent letter?

How many times does it occur?

Format your output as follows:

The most frequent letter in the string is: X

(where "X" represents the most frequent letter and N represents the number of times it occurs)

The most frequent letter is X and it occurs N times in the string.

(where N represents the number of times the most frequent letter occurs)

I don't really care about the formatting as much as I want to know how to code it. I'm taking principles of Java, so I'm at a beginner level, therefore I can't use the char method because I didn't take it yet. I need nested for loops. This is the incomplete code I've coded:

class Main {

  public static void main(String[] args) {

    String sentence = "IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS";

    // write your code here

    String letter = ltr;

    int count = 0;

      for (int i = 0; i < sentence.length(); i  ) {

        String ltr = sentence.substring(i, i   1);

        if (!(ltr.equals(" "))) {

          for (int j = 0; j < sentence.length(); j  ) {

            String ltr2 = sentence.substring(j, j   1);

              if (ltr.equals(ltr2)) {

                count  = 1;

              }

            if (count  ) {

              

            }

          System.out.println((i)   " "   ltr   " "   (j)   " "   ltr2);

          }

        }

      }

      System.out.println(count);

  }// end of main

}// end of class

CodePudding user response:

I have one approach in mind of using with Map.

import java.util.HashMap;
import java.util.Map;

public class MostFrequentChar {

    public static void main(String[] args) {
        String sentence = "IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS";

        mostFrequentCharacter(sentence);
    }

    private static void mostFrequentCharacter(String sentence) {
        Map<Character, Integer> countMap = new HashMap<>();
        int count = Integer.MIN_VALUE;
        Character character = null;
        for (int i = 0; i < sentence.length(); i  ) {
            if (!Character.isSpaceChar(sentence.charAt(i))) {
                countMap.put(sentence.charAt(i),
                        countMap.getOrDefault(sentence.charAt(i), 0)   1);
                if (countMap.get(sentence.charAt(i)) > count) {
                    count = countMap.get(sentence.charAt(i));
                    character = sentence.charAt(i);
                }
            }
        }
        System.out.println(character   " occurs at "   count);

    }
}

In this approach I am keeping the count of each and every character. And also checking which one is occurring most. With this approach we can even sort the occurrence of the chars if needed for further analysis.

CodePudding user response:

Possible solution:

public static void main(String[] args) {

  var sentence = "IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS";
  // remove blanks, split into characters (which are String by themself)
  var characters = sentence.replaceAll("\\s","").split("");
  var map = new HashMap<String, Integer>();

  for (var c : characters) {
    if (map.containsKey(c)) {
      // increment
      map.put(c, map.get(c)   1);
    } else {
      // initial
      map.put(c, 1);
    }
  }

  // get all Map entries as List
  var list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

  // sort by entry's value
  Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

    @Override
    public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
      return o2.getValue().compareTo(o1.getValue());
    }

  });

  // output top element
  System.out.println(list.get(0));
}

Output: E=12

  • Related