Mar 14, 2016

Lab Coat Challenge 6: Quick Mode

Hey there dev's and dev's-to-be! We have a 'Quick' Lab Coat Challenge for you. Review the challenge, take a look at the starter code, and then give it your best shot. Get the right answer and earn your Lab Coat Badge.


Today’s challenge has to do with finding the mode of a given set of integers


Using the smallest amount of code (least statements - no run on lines of multiple statements!), determine what the “mode” (most frequent number) of a set of integers is. For more information on mode go to:  For this challenge, if the solution is multimodal (having more than one mode), choose the lowest number of the results (IE:  If 2, 3, and 10 are modes for a set of integers, the solution will be 2).

Starter Code

You must use the following code and can only add to the code provided below.

/*  LAB COAT CHALLENGE: What is the minimum amount of lines
   needed in order to find the mode of the following list
   of integers? */

//  lists of integers to be used in the asserts
list<Integer> numbers1 = new list<Integer> {2,5,1,2,7,1,2,6,4,6,2,3,8,9};
list<Integer> numbers2 = new list<Integer> {2,5,1,6,7,8,1,5};
list<Integer> numbers3 = new list<Integer> {5,4,7,1,7,2,3,5,4};
list<Integer> numbers4 = new list<Integer> {5,4,7,2,1,7,1,2,5,4};

//  Test your code
system.assertEquals(mode(numbers1), new set<Integer> {2});
system.assertEquals(mode(numbers2), new set<Integer> {1,5});
system.assertEquals(mode(numbers3), new set<Integer> {4,5,7});
system.assertEquals(mode(numbers4), new set<Integer>());


//   Implement the following method
set<Integer> mode(list<Integer> listOfIntegersToTest) {
   set<Integer> returnValues = new set<Integer>();

   // Your code here

   return returnValues;