Remove repeated elements from ArrayList in Java
Prerequisite: ArrayList in Java
Given 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:
- Using a Set: Since Set is a collection which do not includes any duplicate elements. Hence the solution can be achieved with the help of a Set.
Approach:
- Get the ArrayList with repeated elements.
- Convert the ArrayList to Set.
- Now convert the Set back to ArrayList. This will remove all the repeated elements.
Below is the implementation of the above approach:
// Java code to illustrate remove duolicate// of ArrayList using hashSet<> methodimportjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create a ArrayList String typeArrayList<String>gfg =newArrayList<String>();// Initialize an ArrayListgfg.add("Geeks");gfg.add("for");gfg.add("Geeks");// print ArrayListSystem.out.println("Original ArrayList : "+ gfg);// -----Using LinkedHashSet-----System.out.println("\nUsing LinkedHashSet:\n");// create a set and copy all value of listSet<String> set =newLinkedHashSet<>(gfg);// create a list and copy all value of setList<String> gfg1 =newArrayList<>(set);// print ArrayListSystem.out.println("Modified ArrayList : "+ gfg1);// -----Using HashSet-----System.out.println("\nUsing HashSet:\n");// create a set and copy all value of listSet<String> set1 =newHashSet<>(gfg);// create a list and copy all value of setList<String> gfg2 =newArrayList<>(set);// print ArrayListSystem.out.println("Modified ArrayList : "+ gfg2);}}chevron_rightfilter_noneOutput:Original ArrayList : [Geeks, for, Geeks] Using LinkedHashSet: Modified ArrayList : [Geeks, for] Using HashSet: Modified ArrayList : [Geeks, for]
- Using Java 8 Lambdas:
Approach:
- Get the ArrayList with repeated elements.
- Convert the ArrayList to Stream using stream() method.
- Set the filter condition to be distinct using distinct() method.
- Collect the filtered values as List using collect() method. This list will be with repeated elements removed
Below is the implementation of the above approach:
Example:
// Java code to illustrate remove duolicate// of ArrayList using hashSet<> methodimportjava.util.*;importjava.util.stream.Collectors;publicclassGFG {publicstaticvoidmain(String args[]){// create a ArrayList String typeArrayList<String>gfg =newArrayList<String>();// Initialize an ArrayListgfg.add("Geeks");gfg.add("for");gfg.add("Geeks");// print ArrayListSystem.out.println("Original ArrayList : "+ gfg);// create a list and copy all distinct value of listList<String> gfg1 = gfg.stream().distinct().collect(Collectors.toList());// print modified listSystem.out.println("Modified List : "+ gfg1);}}chevron_rightfilter_noneOutput:Original ArrayList : [Geeks, for, Geeks] Modified List : [Geeks, for]
Recommended Posts:
- Remove all elements from the ArrayList in Java
- How to Remove Duplicates from ArrayList in Java
- How to remove an element from ArrayList in Java?
- ArrayList and LinkedList remove() methods in Java with Examples
- Get first and last elements from ArrayList in Java
- Remove elements from a List that satisfy given predicate in Java
- ArrayList of ArrayList in Java
- Java.util.ArrayList.addall() method in Java
- Arraylist.contains() in Java
- ArrayList in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.Arraylist.indexOf() in Java
- ArrayList trimToSize() in Java with example
- Initialize an ArrayList in Java
- ArrayList vs. HashMap in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



