ArrayList duplicates removal : <p>I have an Arraylist with pings, these are dates linked to a name, I want to delete all duplicates of the names and keep the closest date of the name.</p>

<p><strong>Code</strong></p>

<pre><code>private ArrayList &lt;String&gt; deleteDuplicates() {
  ArrayList &lt;Ping&gt; tempPings = new ArrayList &lt; Ping &gt; ();
  tempPings.addAll(jaws.pastMonth());
  for (int i = 0; i &lt; tempPings.size(); i++) {
    Ping tempPing = tempPings.get(i);
    for (int j = i + 1; j &lt; tempPings.size() - 1; j++) {
      Ping tempPing2 = tempPings.get(j);
      if (tempPing.getName().equals(tempPing2.getName())) {
        if (changePingToDate(tempPing2).before(changePingToDate(tempPing))) {
          tempPings.remove(j);
        }
      }
    }
  }
  return pingToNames(tempPings);
}
</code></pre>

<p>changePingToDate() is a method to convert the date string into a gregorian calendar.</p>

<p>When I use this code it deletes a high proportion of the duplicates but there are still some remaining each time through the loop. I have also tried it without comparing the dates and still the same problem. Can anyone help?</p>

<p>Thanks for the help!</p>
