Friday, May 13, 2011

Remove Duplicates in Java

Description:
You have an ArrayList with duplicates, and you need to remove them


Solution:

  • Create a HashSet with the ArrayList as the constructor argument:

    HashSet hashSet = new HashSet(myArrayList);
  • If the order is important, use a LinkedHashSet:

    LinkedHashSet  linkedHashSet = new LinkedHashSet(myArrayList);
  • Use the following method to remove duplcates:
    static ArrayList deDupKeepingFirst(ArrayList withDups,
      Comparator comparator,boolean presorted) {
     if (!presorted) {
      Collections.sort(withDups, comparator);
     }
     int size = withDups.size();
    
     ArrayList result = new ArrayList(size);
     if (size <= 1) {
      if (size == 1) {
       result.add(withDups.get(0));
      }
      return result;
     }
     Object prev = withDups.get(0);
     result.add(prev);
    
     for (int i = 1; i < size; i++) {
      Object o = withDups.get(i);
      if (comparator.compare(o, prev) != 0) {
       result.add(o);
       prev = o;
      }
      else
       System.out.println(o.toString());
     }
     return result;
    }
    
    private static class StringComp implements Comparator
    {
     public int compare(Object o1, Object o2)
     {
      return o1.toString().compareToIgnoreCase((o2).toString());
     }
    }
    

No comments:

Post a Comment