title: Count occurrences in array given values of another array
body: <p>I am writing a script for Google Spreadsheet.</p>
  
  <p>Array 1 have a series of names (more than 50 different names)</p>
  
  <pre><code>John, Pete, Paul, ... Michael
  </code></pre>
  
  <p>Array 2 is a series of repeated names from those given 50 (more than 10K in total)</p>
  
  <pre><code>Paul, Michael, Pete, John, Paul, Paul, Pete, Michael, Paul, Michael,... Michael
  </code></pre>
  
  <p>How can I make another Array with the number of occurrences (Array 2) for every given name in Array 1?</p>
  
  <p>Sorting is not possible. Therefore, Array 3 should take into consideration the order of Array 1. In this case, for instance:</p>
  
  <pre><code>1, 2, 3,... 4
  </code></pre>
  
  <p>I have seen &amp; proved how to make it if order is not important (from Array 2, Array 1 is created with unique names and Array 3 contains their occurrences --> <a href="https://stackoverflow.com/questions/5667888/counting-occurences-of-javascript-array-elements">Counting the occurrences of JavaScript array elements</a>), but all my approaches seem to don't work. So far, I have this:</p>
  
  <pre><code>var namesCountArray = [];
  namesArray = ss.getRange("CU3:CU" + rows + "").getValues();
  namesSerieArray = ss.getRange("DF3:DF" + rows + "").getValues();
  
  for(var i=0; i&lt; namesArray.length; i++) {
      var count = 0;
      for(var i = 0; i &lt; namesSerieArray.length; ++i) {
          if(namesSerieArray[i] == namesArray[i])
              count++;
      }
      namesCountArray.push([count]);
  }
  ss.getRange("DB3").setValue(namesCountArray);
  </code></pre>