An ArrayList can contain duplicate or repeated elements. In Java, there are several ways to remove these repeated elements while keeping only one occurrence of each value.
- A Set does not allow duplicate elements, so it can be used to remove repeated values.
- LinkedHashSet removes duplicates while preserving the original insertion order.
Examples:
Input: [1, 2, 2, 3, 4, 4, 4]
Output: [1, 2, 3, 4]Input: [12, 23, 23, 34, 45, 45, 45, 57, 67]
Output: [12, 23, 34, 45, 57, 67]
Methods to Remove Repeated Elements from ArrayList
1. Using LinkedHashSet
A LinkedHashSet is a collection that does not allow duplicate elements and also maintains the insertion order of the elements.
Approach
- Create an ArrayList containing duplicate elements.
- Create a LinkedHashSet from the ArrayList.
- Duplicate elements are automatically removed.
- Convert the LinkedHashSet back into an ArrayList.
- Print the resulting list.
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
public class RemoveDuplicates {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>();
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("Java");
list.add("for");
System.out.println("Original ArrayList: " + list);
LinkedHashSet<String> set
= new LinkedHashSet<>(list);
List<String> result = new ArrayList<>(set);
System.out.println(
"ArrayList after removing duplicates: "
+ result);
}
}
Output
Original ArrayList: [Geeks, for, Geeks, Java, for] ArrayList after removing duplicates: [Geeks, for, Java]
Explanation: LinkedHashSet does not allow duplicate elements. When the ArrayList is converted into a LinkedHashSet, repeated values are removed automatically. It also maintains the insertion order, so the first occurrence of each element is retained.
2. Using HashSet
A HashSet can also be used to remove repeated elements because it does not allow duplicate values.
Approach
- Create an ArrayList containing duplicate elements.
- Convert the ArrayList into a HashSet.
- The duplicate elements are removed automatically.
- Convert the HashSet back into an ArrayList.
- Print the resulting list.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class RemoveDuplicates {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(10);
list.add(30);
list.add(20);
list.add(40);
System.out.println("Original ArrayList: " + list);
HashSet<Integer> set = new HashSet<>(list);
List<Integer> result = new ArrayList<>(set);
System.out.println(
"ArrayList after removing duplicates: "
+ result);
}
}
Output
Original ArrayList: [10, 20, 10, 30, 20, 40] ArrayList after removing duplicates: [20, 40, 10, 30]
Explanation: The HashSet stores only unique elements. Therefore, when the ArrayList is converted into a HashSet, all repeated values are removed.
Note: If you need to preserve the original order, use LinkedHashSet instead of HashSet.
3. Using Stream distinct() Method
Java 8 introduced the Stream API, which provides the distinct() method to remove duplicate elements from a stream.
Approach
- Create an ArrayList containing duplicate elements.
- Convert the ArrayList into a stream using stream().
- Use the distinct() method to remove duplicate elements.
- Collect the resulting elements into a new List.
- Print the resulting list.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicates{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>();
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("Java");
list.add("for");
System.out.println("Original ArrayList: " + list);
List<String> result= list.stream().distinct().collect(Collectors.toList());
System.out.println("ArrayList after removing duplicates: "+ result);
}
}
Output
Original ArrayList: [Geeks, for, Geeks, Java, for] ArrayList after removing duplicates: [Geeks, for, Java]
Explanation: The stream() method converts the ArrayList into a stream. The distinct() method removes repeated elements from the stream.