Sunday, June 22, 2025
HomeJavaThe way to Take away Duplicates from ArrayList in Java

The way to Take away Duplicates from ArrayList in Java [Example]


ArrayList is the preferred implementation of the Checklist interface from Java’s Assortment framework, but it surely permits duplicates. Although there may be one other assortment referred to as Set which is primarily designed to retailer distinctive components, there are conditions while you obtain a Checklist like ArrayList in your code and it is advisable to make sure that it does not comprise any duplicate earlier than processing. Since with ArrayList you can’t assure uniqueness, there isn’t any different selection however to take away repeated components from ArrayList. 
There are a number of methods to do that, you’ll be able to observe the method we used for eradicating duplicates from array in Java, the place we loop by way of array and inserting every factor in a Set, which ensures that we discard duplicate as a result of Set does not enable them to insert, or you may as well use take away technique of ArrayList to do away with them, when you discovered that these are duplicates.

Btw, the only method to take away repeated objects from ArrayList is to repeat them to a Set e.g. HashSet after which copy it again to ArrayList. This can take away all duplicates with out writing any extra code.

One factor to famous is that, if unique order of components in ArrayList is vital for you, as Checklist maintains insertion order, it is best to use LinkedHashSet as a result of HashSet does not present any ordering assure.

If you’re utilizing deleting duplicates whereas iterating, be sure you use Iterator’s take away() technique and never the ArrayList one to keep away from ConcurrentModificationException.  On this tutorial we’ll see this method to take away duplicates.

Java Program to Take away duplicates from ArrayList

Right here is our pattern program to discover ways to take away duplicates from ArrayList. The steps adopted within the under instance are:

  • Copying all the weather of ArrayList to LinkedHashSet. Why we select LinkedHashSet? As a result of it removes duplicates and maintains the insertion order.
  • Emptying the ArrayList, you should use clear() technique to take away all components of ArrayList and begin contemporary. 
  • Copying all the weather of LinkedHashSet (non-duplicate components) to the ArrayList. 

Please discover under the whole code :

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Checklist;
import java.util.Set;


/**
 * Java Program to take away repeated components from ArrayList in Java.
 *
 * @creator WINDOWS 8
 */

public class ArrayListDuplicateDemo{
 
   
    public static void major(String args[]){
   
        // creating ArrayList with duplicate components
        Checklist<Integer> primes = new ArrayList<Integer>();
       
        primes.add(2);
        primes.add(3);
        primes.add(5);
        primes.add(7);  //duplicate
        primes.add(7);
        primes.add(11);
       
        // let's print arraylist with duplicate
        System.out.println("record of prime numbers : " + primes);
       
        // Now let's take away duplicate factor with out affecting order
        // LinkedHashSet will assured the order and because it's set
        // it is not going to enable us to insert duplicates.
        // repeated components will robotically filtered.
       
        Set<Integer> primesWithoutDuplicates
                   = new LinkedHashSet<Integer>(primes);
       
        // now let's clear the ArrayList in order that we are able to 
        // copy all components from LinkedHashSet
        primes.clear();
       
        // copying components however with none duplicates
        primes.addAll(primesWithoutDuplicates);
       
        System.out.println("record of primes with out duplicates : " + primes);
       
    }
 
}

Output
record of prime numbers : [2, 3, 5, 7, 7, 11]
record of primes with out duplicates : [2, 3, 5, 7, 11]

On this instance, you’ll be able to see we now have created an ArrayList and added numbers into it, all prime numbers. We added ‘7’ twice, in order that it change into duplicate. Now we print the ArrayList and you’ll see that it incorporates quantity 7 twice.

How to remove duplicates from ArrayList in Java
After that we created a LinkedHashSet from our ArrayList, clear our unique ArrayList after which added all components from set to the record. This time we should have no duplicates as a result of Set does not enable them and they need to have filtered when components copied from ArrayList to HashSet by Java. That is proved by printing the ArrayList once more, now it does not comprise 7 twice, however solely as soon as.

That is all about find out how to take away duplicates from ArrayList in Java. Although there are a number of methods to do that, I believe utilizing LinkedHashSet is the only one as a result of its easy and likewise protect the order of components.

If you’re eager about lincomes ArrayList, it is best to take a look at my following tutorials :

  • What’s distinction between two take away() strategies of ArrayList class? (reply)
  • What’s the appropriate strategy to take away objects from ArrayList whereas Iterating? (reply)
  • The way to do away with repeated components from ArrayList? (resolution)
  • The way to reverse an ArrayList in Java? (resolution)
  • The way to synchronize ArrayList in Java? (reply)
  • Distinction between Array and ArrayList in Java? (reply)
  • What’s the distinction between ArrayList and HashSet in Java? (reply)
  • What’s the distinction between HashMap and ArrayList? (reply)
  • The way to convert String ArrayList to String Array in Java? (reply)
  • Newbies Information to ArrayList in Java (information)
  • The way to get a sublist from ArrayList in Java? (program)
  • When to make use of ArrayList over LinkedList in Java? (reply)
  • The way to create and initialize ArrayList in a single line? (trick)
  • The way to type ArrayList of Integers in ascending order? (resolution)
  • What’s distinction between Vector and ArrayList in Java? (reply)
  • The way to loop ArrayList in Java? (resolution)
  • The way to convert an ArrayList to String in Java? (resolution)
  • Array’s size() vs ArrayList measurement() technique (learn right here)
  • What’s CopyOnWriteArrayList in Java? When do you utilize it? (reply)
  • How and when to make use of ArrayList in Java? (reply)
  • The way to make read-only ArrayList in Java? (trick)
  • 3 methods to traverse Checklist in Java? (examples)
  • The way to convert Checklist to Set in Java? (instance)

Thanks for studying this ArrayList tutorial. You have got seen how straightforward it’s to take away duplicate components from ArrayList in Java, you simply want to alter the info construction. In truth, in the event you want distinctive components to want a Set vs Checklist however generally you additionally want advantages of ArrayList like quick search in fixed time and that is the place this method will help you. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments