title: Write a mode method in Java to find the most frequently occurring element in an array
body: <p>The question goes:</p>
  <blockquote>
  <p>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>
  </blockquote>
  <p>Below is my code that almost works except for single-element arrays</p>
  <pre><code>public static int mode(int[] n)
  {
      Arrays.sort(n);
      
      int count2 = 0;
      int count1 = 0;
      int pupular1 =0;
      int popular2 =0;
      
      
      for (int i = 0; i &lt; n.length; i++)
      {
              pupular1 = n[i];
              count1 = 0;    //see edit
          
          for (int j = i + 1; j &lt; n.length; j++)
          {
              if (pupular1 == n[j]) count1++;
          }
          
          if (count1 &gt; count2)
          {
                  popular2 = pupular1;
                  count2 = count1;
          }
          
          else if(count1 == count2)
          {
              popular2 = Math.min(popular2, pupular1);
          }
      }
      
      return popular2;
  }
  </code></pre>
  <p><strong>Edit</strong>: finally figured it out. Changed <code>count1 = 0;</code> to <code>count1 = 1;</code> everything works now!</p>