- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Remove Repeated Element from An ArrayList
In this article, we will understand how to remove repeated element from an array-list. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
Below is a demonstration of the same −
Suppose our input is −
The list is defined as: [Java, Scala, JavaScript, Scala]
The desired output would be −
The list after removing the duplicates is: [Java, Scala, JavaScript]
Algorithm
Step 1 - START Step 2 - Declare an ArrayList namely input_list and declare a set namely temp. Step 3 - Define the values. Step 4 - Convert the list to a set Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.*; public class Demo { public static void main(String args[]) { ArrayList<String> input_list = new ArrayList<String>(); input_list.add("Java"); input_list.add("Scala"); input_list.add("JavaScript"); input_list.add("Scala"); System.out.println("The list is defined as: " + input_list); Set<String> temp = new LinkedHashSet<>(input_list); List<String> result_list = new ArrayList<>(temp); System.out.println("The list after removing the duplicates is: " + result_list); } }
Output
The list is defined as: [Java, Scala, JavaScript, Scala] The list after removing the duplicates is: [Java, Scala, JavaScript]
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.*; public class Demo { static void remove_duplicates(ArrayList<String> input_list){ Set<String> temp = new LinkedHashSet<>(input_list); List<String> result_list = new ArrayList<>(temp); System.out.println("The list after removing the duplicates is: " + result_list); } public static void main(String args[]) { ArrayList<String> input_list = new ArrayList<String>(); input_list.add("Java"); input_list.add("Scala"); input_list.add("JavaScript"); input_list.add("Scala"); System.out.println("The list is defined as: " + input_list); remove_duplicates(input_list); } }
Output
The list is defined as: [Java, Scala, JavaScript, Scala] The list after removing the duplicates is: [Java, Scala, JavaScript]
- Related Articles
- How to remove an element from ArrayList in Java?
- How to remove an element from ArrayList or, LinkedList in Java?
- How to remove element from ArrayList in Java?
- Remove an element from an ArrayList using the ListIterator in Java
- Java Program to Remove duplicate elements from ArrayList
- How to remove duplicates from an ArrayList in Java?
- Java Program to remove an element from List with ListIterator
- Retrieve an element from ArrayList in Java
- Remove duplicate items from an ArrayList in Java
- How to remove a SubList from an ArrayList in Java?
- How to remove the redundant elements from an ArrayList object in java?
- How to remove an element from an array in Java
- Remove an element from IdentityHashMap in Java
- Swift Program to Remove an Element from the Set
- How to remove an element from a Java List?

Advertisements