Java Program to Remove duplicate elements from ArrayList
What is an Array List in Java.
The Java Collection framework includes a dynamic data structure called an ArrayList.The elements are stored in a dynamic array using the Java ArrayList class.
Similar to an array, but with no size restrictions. At any time, we can add or remove components.You can store a group of elements in a resizable array since it implements the List interface.
To know more about Java program to Remove duplicate elements from Array list read the complete article.
Java Set Interface
The Java Collections Framework’s java.util.Set interface extends the java.util.Collection interface, which prohibits duplicate elements and represents a collection of distinct components.
The Set interface offers ways to traverse a set’s elements, as well as ways to add, remove, and check for the presence of components.
- To remove duplicate elements from an ArrayList, you can convert the ArrayList to a Set, since sets do not allow duplicate elements. You can then convert the Set back to an ArrayList if necessary.
- ArrayList is not synchronized, so if multiple threads access an ArrayList simultaneously, it must be synchronized externally.
- Set does not maintain the order of elements, they can be stored and retrieved in any order.
- Some commonly used Set implementations include HashSet, TreeSet and LinkedHashSet.
- Set provides methods like add(), remove(), size(), isEmpty(), contains(), clear(), etc.
Let’s look at a Java program to Remove duplicate elements from Array list is used to perform an operation.
Example: Java Program to Remove duplicate elements from ArrayList using Set
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(1); list.add(2); System.out.println("Original List: " + list); Set set = new HashSet<>(list); list.clear(); list.addAll(set); System.out.println("List after removing duplicates: " + list); } }
Output
Original List: [1, 2, 3, 1, 2] List after removing duplicates: [1, 2, 3]
- A List named list is created with duplicate elements.
- A HashSet is created using the list as the constructor argument.
- The clear method is used to remove all elements from the list.
- The addAll method is used to add all unique elements from the set to the list.
- The list after removing duplicates is printed.
Example 2 : 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 Main { 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); } public static void main(String[] args) { System.out.println("The required packages have been imported"); ArrayList input_list = new ArrayList<>(Arrays.asList(100, 200, 225, 275, 350, 475, 500, 650, 900)); 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: [100, 200, 225, 275, 350, 475, 500, 650, 900] The list with no duplicates is: [100, 200, 225, 275, 350, 475, 500, 650, 900]
Example 3: Java Program to Remove duplicate elements from ArrayList using Stream
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Listnumbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(1); numbers.add(2); List distinctNumbers = numbers.stream() .distinct() .collect(Collectors.toList()); System.out.println("Original List: " + numbers); System.out.println("Distinct List: " + distinctNumbers); } }
Output
Original List: [1, 2, 3, 1, 2] Distinct List: [1, 2, 3]
- Imports java.util.ArrayList and java.util.stream.Collectors classes to use in the program.
- Creates an ArrayList named list and adds some integer elements to it.
- Prints the original ArrayList list to the console.
- Creates another ArrayList named uniqueList.
- The code list.stream().distinct() creates a stream from the elements of the list ArrayList, and the distinct() method is used to eliminate duplicates from the stream.
- The collect() method is used to collect the elements from the stream into a collection, specifically ArrayList::new is used to create a new ArrayList to store the distinct elements.
- Finally, the uniqueList is printed to the console, showing the list with duplicates removed.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment