Sum all same elements in one array
<p>how can I sum all same elements in one array? For example I have an array:   </p>

<pre><code>  [20,20,20,10,10,5,1]
</code></pre>

<p>How can I make it <code>[60,20,5,1]</code>?</p>

<p>Here's what I have tried so far:</p>

<pre><code>var money = [20, 20, 20, 10, 10, 5, 1];
for (var i = 0; i &lt; money.length; i++) {
  if (money[i] == money[i + 1]) {
    money[i] += money[i + 1];
    money.splice(money.indexOf(money[i + 1]), 1);
  }
}
</code></pre>