3 ways to remove duplicates in List

Summary

Developers often need to extract distinct values from an ArrayList. One efficient approach involves leveraging a HashSet: either by initializing a HashSet with the ArrayList and then repopulating the list, or by iterating through the list and adding elements to a new list only if they can be successfully added to a tracking HashSet. A third, less efficient method employs nested loops to compare and remove duplicate elements directly within the ArrayList, iterating backwards in the inner loop to handle shifting indices correctly. These techniques offer varying performance characteristics for de-duplicating collections in Java.

Frequently, we may have an ArrayList which stores many values, and we need to process the ArrayList and get what are the distinct values in the list, or we may want to count occurrence of each value in the ArrayList. We can remove the duplicates in a few ways. Here we propose 3 methods :

    public static void main(String[] args){
        //SuperClass sub=new SubClass();
        
        String[] ar = { "dd", "c", "dd", "ff", "b", "e", "e" };
        ArrayList list = new ArrayList();
        for (int i = 0; i < ar.length; i++) {
            list.add(ar[i]);
        }
        System.out.println("Before running : ");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
        /* First method */
        /**
         * A set will only store non-duplicate value
         */
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        /* First method */

        /* Second method */
        /**
         * Second method also uses the set, if the set has the value existed already,
         * the add method will return false, so the value will not be added to
         * the newList
         */
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        /* Second method */

        /* Third method */
        /**
         * Normal way, remove the duplicate value from the list using two for
         * loops to check on the same list
         */
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        /* Third method */

        System.out.println("After running:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
    }

There should be other simple, efficient ways achieving this. We will add them.

Reference : http://www.javamm.com/?p=8149

JAVA CLEAR LIST DUPLICATE

  RELATED

  COMMENTS

0

No comment for this article.