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<> methodÂÂimportjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// create a ArrayList String type       ÂArrayList<String>           Âgfg =newArrayList<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 =newLinkedHashSet<>(gfg);       Â// create a list and copy all value of set       ÂList<String> gfg1 =newArrayList<>(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 =newHashSet<>(gfg);       Â// create a list and copy all value of set       ÂList<String> gfg2 =newArrayList<>(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]
- 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<> methodÂÂimportjava.util.*;importjava.util.stream.Collectors;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// create a ArrayList String type       ÂArrayList<String>           Âgfg =newArrayList<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]
