VOOZH about

URL: https://www.geeksforgeeks.org/java/remove-repeated-elements-from-arraylist-in-java/

⇱ Remove repeated elements from ArrayList in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove repeated elements from ArrayList in Java

Last Updated : 11 Jul, 2025
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:
    1. Get the ArrayList with repeated elements.
    2. Convert the ArrayList to Set.
    3. Now convert the Set back to ArrayList. This will remove all the repeated elements.
    Below is the implementation of the above approach:
    Output:
    Original ArrayList : [Geeks, for, Geeks]
    
    Using LinkedHashSet:
    
    Modified ArrayList : [Geeks, for]
    
    Using HashSet:
    
    Modified ArrayList : [Geeks, for]
    
  • Using Java 8 Lambdas: Approach:
    1. Get the ArrayList with repeated elements.
    2. Convert the ArrayList to Stream using stream() method.
    3. Set the filter condition to be distinct using distinct() method.
    4. 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:
Output:
Original ArrayList : [Geeks, for, Geeks]
Modified List : [Geeks, for]
Comment