title: Finding Multiple Modes In An Array
body: <p>I'm trying to write a java method which finds all the modes in an array. I know there is a simple method to find the mode in an array but when there are more than one single mode my method outputs only one of them. I've tried to find a way but am nit sure how to approach this problem. Can anyone help me out to find all the modes in the array? Thanks.</p>
  
  <p>Yes here is my code which outputs only one mode even if multiple modes exist.</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 maxValue;
  }
  </code></pre>
  
  <p>okay here's an example:
  30
  30
  30
  34
  34
  23</p>
  
  <p>In this set of numbers there is only one mode, which is 30.</p>
  
  <p>30
  30
  30
  34
  34
  34
  23</p>
  
  <p>But in this set there are two modes, 30 and 34. I want my code to be able to output both of them, whereas it only prints one. It prints only 30.</p>