Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method that accepts an array of String objects, and returns the S

ID: 3771882 • Letter: W

Question

Write a static method that accepts an array of String objects, and returns the String value that occurs most often in the array. The method should have the following signature:

public static String mostOftenOcuringValue(String[] strings)

If values are tied for most-often-occuring, then you can break the tie arbitrarily (any of the tied values is acceptable).

There are different approaches to solving this problem, but the best approach is to use a dictionary with Strings and keys, and Integers as values.

Explanation / Answer

This below Java code will solve the mosFrequest Strings given in the Strings array in the method using Dictonary collection..See the below code comments with code.

public static String mostOftenOcuringValue(String[] strings) {
  
// Create a dictionary using strings as key, and frequency as value
Map<String, Integer> dictionary = new HashMap<String, Integer>();
   // Find the frequent word in the given Strings array
for (String strings : stringss) {
if (dictionary.containsKey(strings)) {
int frequency = dictionary.get(strings);
dictionary.put(strings, frequency + 1);
} else {
dictionary.put(strings, 1);
}
}

  
int max = 0;
String mostFrequentstrings = "";
Set<Entry<String, Integer>> set = dictionary.entrySet();
for (Entry<String, Integer> entry : set) {
if (entry.getValue() > max) {
max = entry.getValue();
mostFrequentstrings = entry.getKey();
}
}

   // Retun the most frequent strings in the given strings array
return mostFrequentstrings;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote