Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing java/03-arrays/Commonest by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<div>
Write a program <code>Commonest</code> that takes any number of command-line
arguments, which will be strings, and prints out the one that occurs most
often. For example <code>java Commonest cat dog monkey cat goose</code>
should print out <code>cat</code> and <code>java Commonest a a b b b</code> should print out <code>b</code>.
</div>
<div>
If more than one word is the commonest, print out the first one that 
occurs. Don't forget that strings have to be compared with <code>.equals</code>
instead of <code>==</code>.
</div>
Public permissions remove
Engine
Template / Reference solution
 
public static void main(String[] args) {
   String commonest = "";    // just a placeholder for now
   int commonest_count = -1; // same
   int n = args.length;
   for (int i=0; i<n; i++) {
      // count how many times args[i] occurred
\[
      int count=0;
      for (int j=0; j<n; j++) {       // look everywhere
         if (args[i].equals(args[j])) // when you find it
            count++;                  // count it
      }
]\
      // we found a new commonest word
      if (count > commonest_count) {
         commonest = args[i];
         commonest_count = count;
      }
   }
   System.out.println(commonest);
}
Java test suite
See manual
 
testMain("dog", "cat", "fish", "bird", "cat");
testMain("a", "a", "a", "b", "b", "a", "b", "b", "b");
testMain("the", "commonest", "word", "in", "the", "English", "language");
testMain("first", "second", "third");
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.