title: Finding the most frequent value in an array and then choosing the lowest value if there is a tie
body: <p>I have the following program which looks up an array "a" and then output the value in the array which appears the most frequently. However one other condition that I would like to implement is that in the case of a tie where two different values appear an equal amount of times, the lowest value gets output.</p>
  
  <p>So for the following code with an array:</p>
  
  <pre><code>int a[] = {34, 34, 20, 20, 15};
  </code></pre>
  
  <p>It outputs 34 however I want it to output 20 instead, since that is a lower value and appears just as many times in the array.</p>
  
  <pre><code>public class Arrays3 {
      public static void main(String[] args){
          int a[] = {34, 34, 20, 20, 15};
          mode(a);
      }
      public static int mode(int[] a) {
          int[] counts = new int[101];
          int maxCount = 0;
          int maxKey = 0;
  
          for(int i = 0; i &lt; a.length; i++) {
              counts[a[i]]++;
              if(counts[a[i]] &gt; maxCount) {
                  maxCount = counts[a[i]];
                  maxKey = a[i];
              }
          }
          System.out.println(maxKey);
          return maxKey;
      }
  }
  </code></pre>