
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Duplicate Elements from ArrayList in Java
In this article, we will understand how to remove duplicate elements from ArrayList. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified.
What is an ArrayList?
ArrayList is part of the collections framework and comes from java.util package. It gives us dynamic arrays, which can be slower than regular arrays but are useful when we need to change the array a lot. The main benefit is that, unlike regular arrays, you don't have to set the size when creating an ArrayList because it automatically changes size as you add or remove elements.
Methods to remove duplicate elements from ArrayList
The following are the methods to remove duplicate elements from ArrayList ?
- Using LinkedHashSet
- Using User-Defined Function
Remove duplicate elements from ArrayList using LinkedHashSet
LinkedHashSet: A LinkedHashSet is a part of the Java collections framework that maintains the insertion order of elements and removes duplicates.
- Step 1: Define the ArrayList and Import Packages: We first create an ArrayList and initialize it with values.
ArrayList input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300));
System.out.println("The list is defined as: " + input_list);
-
Step 2: Remove Duplicates Using LinkedHashSet: A LinkedHashSet is created and the elements from the ArrayList are added to it. This automatically removes duplicates while maintaining the order of elements.
Settemp_set = new LinkedHashSet<>();
temp_set.addAll(input_list);
-
Step 3: Update the ArrayList: The ArrayList is cleared and updated with the unique elements from the LinkedHashSet.
input_list.clear();
input_list.addAll(temp_set);
System.out.println("\nThe list with no duplicates is: \n" + input_list);
Example 1
The following is an example of a Java program to remove duplicate elements from ArrayList ?
import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); ArrayList<Integer> input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300)); System.out.println("The list is defined as: " + input_list); Set<Integer> temp_set = new LinkedHashSet<>(); temp_set.addAll(input_list); input_list.clear(); input_list.addAll(temp_set); System.out.println("\nThe list with no duplicates is: \n" + input_list); } }
Output
The required packages have been imported The list is defined as: [150, 250, 300, 250, 500, 150, 600, 750, 300] The list with no duplicates is: [150, 250, 300, 500, 600, 750]
Remove duplicate elements from ArrayList using User-Defined Function
User-defined function: In Java, user-defined functions are custom instructions that you can call to do a task. They can take in data and may return a result, but both are optional. Methods can be public, private, or protected.
- Step 1: Define a Function to Remove Duplicates: A static method remove_duplicates is created, which accepts an ArrayList and removes its duplicate elements using a LinkedHashSet
static void remove_duplicates(ArrayList input_list){
Set temp_set = new LinkedHashSet<>();
temp_set.addAll(input_list);
input_list.clear();
input_list.addAll(temp_set);
System.out.println("\nThe list with no duplicates is: \n" + input_list);
}
- Step 2: Define and Initialize the ArrayList: The ArrayList is initialized with duplicate elements, and the remove_duplicates method is called to remove the duplicates.
ArrayList input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300));
System.out.println("The list is defined as: " + input_list);
remove_duplicates(input_list);
Example 2
The following is an example of a Java program to remove duplicate elements from ArrayList ?import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; public class Demo { static void remove_duplicates(ArrayList<Integer> input_list) { Set<Integer> temp_set = new LinkedHashSet<>(); temp_set.addAll(input_list); input_list.clear(); input_list.addAll(temp_set); System.out.println("\nThe list with no duplicates is: \n" + input_list); } public static void main(String[] args) { System.out.println("The required packages have been imported"); ArrayList<Integer> input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300)); System.out.println("The list is defined as: " + input_list); remove_duplicates(input_list); } }
Output
The required packages have been imported The list is defined as: [150, 250, 300, 250, 500, 150, 600, 750, 300] The list with no duplicates is: [150, 250, 300, 500, 600, 750]