title: Finding the Mode of Integers in an Array
body: <p>For this problem, I am to write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value.</p>
  
  <p>For example, if the array passed contains the values {27, 15, 15, 11, 27}, your method should return 15. (Hint: You may wish to look at the Tally program from earlier in this chapter to get an idea of how to solve this problem.)</p>
  
  <p>I am having a problem seeing what is going wrong for a specific input. For instance:</p>
  
  <p>mode({27, 15, 15, 27, 11, 11, 11, 14, 15, 15, 16, 19, 99, 100, 0, 27})  returns 15 which is correct, but mode({1, 1, 2, 3, 3}) returns 3 when it should be 1.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static int mode(int[] input) {
      int returnVal = input[0]; // stores element to be returned
      int repeatCount = 0; // counts the record number of repeats
      int prevRepCnt = 0; // temporary count for repeats
  
      for (int i=0; i&lt;input.length; i++) { // goes through each elem
  
          for (int j=i; j&lt;input.length; j++) { // compares to each elem after the first elem
  
              if (i != j &amp;&amp; input[i] == input[j]) { // if matching values
                  repeatCount++; // gets the repeat count
  
                  if (repeatCount&gt;=prevRepCnt) { // a higher count of repeats than before
                      returnVal=input[i]; // return that element
                  }
                  prevRepCnt = repeatCount; // Keeps the highest repeat record
              }
              repeatCount=0; // resets repeat Count for next comparison
          }
      }
      return returnVal;
  }
  </code></pre>