Java Program to Remove Duplicates from ArrayList2 min read

In this tutorial, we will learn to write a Java program to remove duplicates from an ArrayList, we will learn to remove duplicate elements by various approaches.

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 the program, and duplicates in a list.

We will see two ways to remove duplicates in java ArrayList

  • Using HashSet
  • Using LinkedHashSet

1. Remove duplicate elements using HashSet 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:

The output of removing duplicate elements using HashSet:

After removal of Duplicates
[Rick, Dayrl, Glenn, Carol, Maggie]


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 maintains insertion order. It is the useful property of LinkedHashSet that is used in removing Duplicates elements from ArrayList.

Program:

Output:

After removal of Duplicates
[Rick, Glenn, Dayrl, Carol, Maggie]


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …