title: Recursively aggregate duplicates in a keyed pair of lists
body: <p>I have two lists, <em>where items between lists at matching indexes are linked</em>. The first list provides keys that tie to the second lists values:</p>
  
  <pre><code>List&lt;Double&gt; a1 = [10,20,20,30,10];           // keys
  List&lt;Double&gt; y1 = [2012,2013,2012,2012,2013]; // values
  </code></pre>
  
  <p>I want to remove duplicates from the key (index) list in such a way that when a duplicate is found, the value of the key is added. So for example, if two keys were found with the value 10, I want to replace both keys with a single key of value 20. This process would be repeated until there were no duplicate keys remaining. So I want the output of the lists like this:</p>
  
  <pre><code>List&lt;Double&gt; a1 = [60,30];
  List&lt;Double&gt; y1 = [2012,2013];
  </code></pre>
  
  <p>I have attempted to solve this problem using the following code, but the output is not correct. </p>
  
  <pre><code>y2=new ArrayList&lt;Double&gt;();
  a2 = new ArrayList&lt;Double&gt;();
  String y = "";
  double a = 0;
  
  for (int i = 0; i &lt; y1.size(); i++) {
  
      if (y1.get(i).equals(y)) {
          a = a + y1.get(i);
      } else {
          if (!y.equals("")) {
              y2.add(y);
              a2.add(a);
          }
  
          y = y1.get(i);
          a = a1.get(i);
      }
  }
  
  y2.add(y);
  a2.add(a);
  </code></pre>
  
  <p>Any help appreciated, thanks.</p>