title: Finding modes an an array
body: <p>Does anyone know how to find the modes in an array when there are more then one mode? I have this code that finds one mode. But I'm dealing with an array which has more than one mode, a multimodal array and I have to print each mode exactly once. Here is my code, can someone help me out? Thanks.</p>
  
  <pre><code>public static int mode(int a[])
  {
      int maxValue=0, maxCount=0;
  
      for (int i = 0; i &lt; a.length; ++i)
      {
          int count = 0;
          for (int j = 0; j &lt; a.length; ++j)
          {
              if (a[j] == a[i]) ++count;
          }
  
          if (count &gt; maxCount)
          {
              maxCount = count;
              maxValue = a[i];
          }
      }
  
      return maxCount;
  }
  
  public static Integer[] modes(int a[])
  {
      List&lt;Integer&gt; modes = new ArrayList&lt;Integer&gt;();
      int maxCount=0;
      for (int i = 0; i &lt; a.length; ++i)
      {
          int count = 0;
          for(int j = 0; j &lt; a.length; ++j)
          {
              if (a[j] == a[i]) ++count;
          }
  
          if (count &gt; maxCount)
          {
              maxCount = count;
              modes.clear();
              modes.add(a[i]);
          }
          else if (count == maxCount)
          {
              modes.add(a[i]);
          }
      }
      return modes.toArray(new Integer[modes.size()]);
  }
  </code></pre>