Last Updated : 11 Jul, 2025
Try it on GfG Practice
Prerequisite: ArrayList in JavaGiven an ArrayList, the task is to remove repeated elements of the ArrayList in Java.
Examples:Input: ArrayList = [1, 2, 2, 3, 4, 4, 4] Output: [1, 2, 3, 4] Input: ArrayList = [12, 23, 23, 34, 45, 45, 45, 45, 57, 67, 89] Output: [12, 23, 34, 45, 57, 67, 89]
Below are the various methods to remove repeated elements an ArrayList in Java:
// Java code to illustrate remove duolicate
// of ArrayList using hashSet<> method
import java.util.*;
public class GFG {
public static void main(String args[])
{
// create a ArrayList String type
ArrayList<String>
gfg = new ArrayList<String>();
// Initialize an ArrayList
gfg.add("Geeks");
gfg.add("for");
gfg.add("Geeks");
// print ArrayList
System.out.println("Original ArrayList : "
+ gfg);
// -----Using LinkedHashSet-----
System.out.println("\nUsing LinkedHashSet:\n");
// create a set and copy all value of list
Set<String> set = new LinkedHashSet<>(gfg);
// create a list and copy all value of set
List<String> gfg1 = new ArrayList<>(set);
// print ArrayList
System.out.println("Modified ArrayList : "
+ gfg1);
// -----Using HashSet-----
System.out.println("\nUsing HashSet:\n");
// create a set and copy all value of list
Set<String> set1 = new HashSet<>(gfg);
// create a list and copy all value of set
List<String> gfg2 = new ArrayList<>(set);
// print ArrayList
System.out.println("Modified ArrayList : "
+ gfg2);
}
}
Output:
Original ArrayList : [Geeks, for, Geeks] Using LinkedHashSet: Modified ArrayList : [Geeks, for] Using HashSet: Modified ArrayList : [Geeks, for]
// Java code to illustrate remove duolicate
// of ArrayList using hashSet<> method
import java.util.*;
import java.util.stream.Collectors;
public class GFG {
public static void main(String args[])
{
// create a ArrayList String type
ArrayList<String>
gfg = new ArrayList<String>();
// Initialize an ArrayList
gfg.add("Geeks");
gfg.add("for");
gfg.add("Geeks");
// print ArrayList
System.out.println("Original ArrayList : "
+ gfg);
// create a list and copy all distinct value of list
List<String> gfg1 = gfg.stream()
.distinct()
.collect(Collectors.toList());
// print modified list
System.out.println("Modified List : " + gfg1);
}
}
Output:
Original ArrayList : [Geeks, for, Geeks] Modified List : [Geeks, for]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4