How to remove duplicates from ArrayList in Java2 min read

In this article on Java program to remove duplicates from an ArrayList, we will learn to remove duplicate elements by various approaches.
Let’s begin.

ArrayList is a collection type used mostly in java. It is the list interface from Java’s collection framework that allows duplicates in its list. It provides insertion order, flexibility in program and duplicates in a list.

We will see two way to remove duplicates in java ArrayList

  • Using HashSet
  • Using LinkedHashSet

1. Remove duplicate elements using HasSet in ArrayList in java

HashSet is one of the methods to remove elements from ArrayList. It does not follow insertion order which is the disadvantage of using it because once the duplicates elements are removed, the elements will not be in an insertion order inside ArrayList.

Program:

Output of removing duplicate elements using HasSet:


2. Remove duplicate elements using LinkedHashSet in ArrayList in java

LinkedHashSet is also one of the other methods to remove elements from ArrayList. The advantage of using LinkedHashSet is that it does not allow duplicates and also maintain insertion order. It is the useful property of LinkedHashSet that is used in removing Duplicates elements from ArrayList.

Program:

Output:


Java Programs
Java Interview Programs