How to Remove Duplicates from ArrayList in Java - GeeksforGeeks

11 Dec, 2018
Given an ArrayList with duplicate values, the task is to remove the duplicate values from this ArrayList in Java.
Examples:
Input: List = [1, 10, 2, 2, 10, 3, 3, 3, 4, 5, 5]
Output: List = [1, 10, 2, 3, 4, 5]

Input: List = ["G", "e", "e", "k", "s"]
Output: List = ["G", "e", "k", "s"]

Using IteratorApproach:Get the ArrayList with duplicate values.Create another ArrayList.Traverse through the first arraylist and store the first appearance of each element into the second arraylist using contains() method.The second ArrayList contains the elements with duplicates removed.Below is the implementation of the above approach:// Java program to remove duplicates from ArrayList  import java.util.*;  public class GFG {      // Function to remove duplicates from an ArrayList    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)    {          // Create a new ArrayList        ArrayList<T> newList = new ArrayList<T>();          // Traverse through the first list        for (T element : list) {              // If this element is not present in newList            // then add it            if (!newList.contains(element)) {                  newList.add(element);            }        }          // return the new list        return newList;    }      // Driver code    public static void main(String args[])    {          // Get the ArrayList with duplicate values        ArrayList<Integer>            list = new ArrayList<>(                Arrays                    .asList(1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5));          // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Remove duplicates        ArrayList<Integer>            newList = removeDuplicates(list);          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}Output:ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]
Using LinkedHashSetA better way (both time complexity and ease of implementation wise) is to remove duplicates from an ArrayList is to convert it into a Set that does not allow duplicates. Hence LinkedHashSet is the best option available as this do not allows duplicates as well it preserves the insertion order.Approach:Get the ArrayList with duplicate values.Create a LinkedHashSet from this ArrayList. This will remove the duplicatesConvert this LinkedHashSet back to Arraylist.The second ArrayList contains the elements with duplicates removed.Below is the implementation of the above approach:// Java program to remove duplicates from ArrayList  import java.util.*;  public class GFG {      // Function to remove duplicates from an ArrayList    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)    {          // Create a new LinkedHashSet        Set<T> set = new LinkedHashSet<>();          // Add the elements to set        set.addAll(list);          // Clear the list        list.clear();          // add the elements of set        // with no duplicates to the list        list.addAll(set);          // return the list        return list;    }      // Driver code    public static void main(String args[])    {          // Get the ArrayList with duplicate values        ArrayList<Integer>            list = new ArrayList<>(                Arrays                    .asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));          // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Remove duplicates        ArrayList<Integer>            newList = removeDuplicates(list);          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}Output:ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]
Using Java 8 Stream.distinct()You can use the distinct() method from the Stream API. The distinct() method return a new Stream without duplicates elements based on the result returned by equals() method, which can be used for further processing. The actual processing of Stream pipeline starts only after calling terminal methods like forEach() or collect().Approach:Get the ArrayList with duplicate values.Create a new List from this ArrayList.Using Stream().distinct() method which return distinct object stream.convert this object stream into ListBelow is the implementation of the above approach:// Java program to remove duplicates from ArrayList  import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;  // Program to remove duplicates from a List in Java 8class GFG{    public static void main(String[] args)    {        // input list with duplicates        List<Integer> list = new ArrayList<>(            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));            // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Construct a new list from the set constucted from elements        // of the original list        List<Integer> newList = list.stream()                                      .distinct()                                      .collect(Collectors.toList());          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}Output:ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Using IteratorApproach:Get the ArrayList with duplicate values.Create another ArrayList.Traverse through the first arraylist and store the first appearance of each element into the second arraylist using contains() method.The second ArrayList contains the elements with duplicates removed.Below is the implementation of the above approach:// Java program to remove duplicates from ArrayList  import java.util.*;  public class GFG {      // Function to remove duplicates from an ArrayList    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)    {          // Create a new ArrayList        ArrayList<T> newList = new ArrayList<T>();          // Traverse through the first list        for (T element : list) {              // If this element is not present in newList            // then add it            if (!newList.contains(element)) {                  newList.add(element);            }        }          // return the new list        return newList;    }      // Driver code    public static void main(String args[])    {          // Get the ArrayList with duplicate values        ArrayList<Integer>            list = new ArrayList<>(                Arrays                    .asList(1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5));          // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Remove duplicates        ArrayList<Integer>            newList = removeDuplicates(list);          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}Output:ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Approach:
Get the ArrayList with duplicate values.Create another ArrayList.Traverse through the first arraylist and store the first appearance of each element into the second arraylist using contains() method.The second ArrayList contains the elements with duplicates removed.
Get the ArrayList with duplicate values.
Create another ArrayList.
Traverse through the first arraylist and store the first appearance of each element into the second arraylist using contains() method.
The second ArrayList contains the elements with duplicates removed.
Below is the implementation of the above approach:
// Java program to remove duplicates from ArrayList  import java.util.*;  public class GFG {      // Function to remove duplicates from an ArrayList    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)    {          // Create a new ArrayList        ArrayList<T> newList = new ArrayList<T>();          // Traverse through the first list        for (T element : list) {              // If this element is not present in newList            // then add it            if (!newList.contains(element)) {                  newList.add(element);            }        }          // return the new list        return newList;    }      // Driver code    public static void main(String args[])    {          // Get the ArrayList with duplicate values        ArrayList<Integer>            list = new ArrayList<>(                Arrays                    .asList(1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5));          // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Remove duplicates        ArrayList<Integer>            newList = removeDuplicates(list);          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Using LinkedHashSetA better way (both time complexity and ease of implementation wise) is to remove duplicates from an ArrayList is to convert it into a Set that does not allow duplicates. Hence LinkedHashSet is the best option available as this do not allows duplicates as well it preserves the insertion order.Approach:Get the ArrayList with duplicate values.Create a LinkedHashSet from this ArrayList. This will remove the duplicatesConvert this LinkedHashSet back to Arraylist.The second ArrayList contains the elements with duplicates removed.Below is the implementation of the above approach:// Java program to remove duplicates from ArrayList  import java.util.*;  public class GFG {      // Function to remove duplicates from an ArrayList    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)    {          // Create a new LinkedHashSet        Set<T> set = new LinkedHashSet<>();          // Add the elements to set        set.addAll(list);          // Clear the list        list.clear();          // add the elements of set        // with no duplicates to the list        list.addAll(set);          // return the list        return list;    }      // Driver code    public static void main(String args[])    {          // Get the ArrayList with duplicate values        ArrayList<Integer>            list = new ArrayList<>(                Arrays                    .asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));          // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Remove duplicates        ArrayList<Integer>            newList = removeDuplicates(list);          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}Output:ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

A better way (both time complexity and ease of implementation wise) is to remove duplicates from an ArrayList is to convert it into a Set that does not allow duplicates. Hence LinkedHashSet is the best option available as this do not allows duplicates as well it preserves the insertion order.
Approach:
Get the ArrayList with duplicate values.Create a LinkedHashSet from this ArrayList. This will remove the duplicatesConvert this LinkedHashSet back to Arraylist.The second ArrayList contains the elements with duplicates removed.
Get the ArrayList with duplicate values.
Create a LinkedHashSet from this ArrayList. This will remove the duplicates
Convert this LinkedHashSet back to Arraylist.
The second ArrayList contains the elements with duplicates removed.
Below is the implementation of the above approach:
// Java program to remove duplicates from ArrayList  import java.util.*;  public class GFG {      // Function to remove duplicates from an ArrayList    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)    {          // Create a new LinkedHashSet        Set<T> set = new LinkedHashSet<>();          // Add the elements to set        set.addAll(list);          // Clear the list        list.clear();          // add the elements of set        // with no duplicates to the list        list.addAll(set);          // return the list        return list;    }      // Driver code    public static void main(String args[])    {          // Get the ArrayList with duplicate values        ArrayList<Integer>            list = new ArrayList<>(                Arrays                    .asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));          // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Remove duplicates        ArrayList<Integer>            newList = removeDuplicates(list);          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Using Java 8 Stream.distinct()You can use the distinct() method from the Stream API. The distinct() method return a new Stream without duplicates elements based on the result returned by equals() method, which can be used for further processing. The actual processing of Stream pipeline starts only after calling terminal methods like forEach() or collect().Approach:Get the ArrayList with duplicate values.Create a new List from this ArrayList.Using Stream().distinct() method which return distinct object stream.convert this object stream into ListBelow is the implementation of the above approach:// Java program to remove duplicates from ArrayList  import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;  // Program to remove duplicates from a List in Java 8class GFG{    public static void main(String[] args)    {        // input list with duplicates        List<Integer> list = new ArrayList<>(            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));            // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Construct a new list from the set constucted from elements        // of the original list        List<Integer> newList = list.stream()                                      .distinct()                                      .collect(Collectors.toList());          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}Output:ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

You can use the distinct() method from the Stream API. The distinct() method return a new Stream without duplicates elements based on the result returned by equals() method, which can be used for further processing. The actual processing of Stream pipeline starts only after calling terminal methods like forEach() or collect().
Approach:
Get the ArrayList with duplicate values.Create a new List from this ArrayList.Using Stream().distinct() method which return distinct object stream.convert this object stream into List
Get the ArrayList with duplicate values.
Create a new List from this ArrayList.
Using Stream().distinct() method which return distinct object stream.
convert this object stream into List
Below is the implementation of the above approach:
// Java program to remove duplicates from ArrayList  import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;  // Program to remove duplicates from a List in Java 8class GFG{    public static void main(String[] args)    {        // input list with duplicates        List<Integer> list = new ArrayList<>(            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));            // Print the Arraylist        System.out.println("ArrayList with duplicates: "                           + list);          // Construct a new list from the set constucted from elements        // of the original list        List<Integer> newList = list.stream()                                      .distinct()                                      .collect(Collectors.toList());          // Print the ArrayList with duplicates removed        System.out.println("ArrayList with duplicates removed: "                           + newList);    }}
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

29AjayKumar
Java-ArrayList
Java-Collections
java-LinkedHashSet
Java-List-Programs
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples
Reverse a string in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java