
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Remove duplicate elements from ArrayList
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.
Below is a demonstration of the same −
Suppose our input is −
Input list : [150, 250, 300, 250, 500, 150, 600, 750, 300]
The desired output would be −
The list with no duplicates is: [150, 250, 300, 500, 600, 750]
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 – Create an ArrayList of integer values and initialize elements in it. Step 5 - Display the ArrayList on the console. Step 6 - Create another linkedhashset of integers. Step 7 - Use the ‘addAll’ method to include elements from previous ArrayList into it as elements. Step 8 - Since it is a set, it only adds the unique values. Step 9 - Clear the elements of the ArrayList. Step 10- Display the set on the console with unique elements. Step 11- Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
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]
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
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]
- Related Articles
- Remove duplicate items from an ArrayList in Java
- C# program to remove duplicate elements from a List
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- Python Program to remove duplicate elements from a dictionary
- Remove all elements from the ArrayList in Java
- Java Program to Remove Repeated Element from An ArrayList
- Python program to remove duplicate elements from a Circular Linked List
- How to remove the redundant elements from an ArrayList object in java?
- Python program to remove duplicate elements from a Doubly Linked List\n
- How to remove all elements of ArrayList in Java?
- Java Program to Remove elements from the LinkedList
- Remove duplicate elements in Java with HashSet
- Remove all elements from the ArrayList in C#
- How to remove element from ArrayList in Java?

Advertisements