title: Highest occurrence in an array or first selected
body: <p>I'm trying to get the highest occurrence of a array value, and if there's an equal occurrence, I should get the first selected value of the equal occurrences.</p>
  
  <p>Example:</p>
  
  <p><code>var array = ['25', '50', 'a', 'a', 'b', 'c']</code></p>
  
  <p>In this case I should get <code>a</code></p>
  
  <p><code>var array = ['75', '100', 'a', 'b', 'b', 'a']</code></p>
  
  <p>In this case I also should get <code>a</code></p>
  
  <p>I've done my fair share of searching and found a couple of helpfull posts, these for example:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array">Get the element with the highest occurrence in an array</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/31227687/find-the-most-frequent-item-of-an-array-not-just-strings">Find the most frequent item of an array (not just strings)</a></p></li>
  </ul>
  
  <p>Somehow I can't seem to modify these examples to work for my case.</p>
  
  <p>Right now I'm using the code below, but its returning the last selected equal occurrence, instead of the first. (credit <a href="https://stackoverflow.com/users/1238344/emissary">https://stackoverflow.com/users/1238344/emissary</a>)</p>
  
  <pre><code>function mostFrequent(array){
    return array.sort(function(a,b){
      return array.filter(function(v){ return v===a }).length
        - array.filter(function(v){ return v===b }).length
    }).pop();
  }
  </code></pre>
  
  <p>Any help with this is welcome.</p>