How to Remove Duplicates from ArrayList in Java
Last Updated :
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 Iterator
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.
Below is the implementation of the above approach:
Java
// 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 LinkedHashSet
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 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
// 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 List
Below is the implementation of the above approach:
Java
// 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 8
class 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]
Similar Reads
Java List Interface The List Interface in Java extends the Collection Interface and is a part of the java.util package. It is used to store the ordered collections of elements. In a Java List, we can organize and manage the data sequentially. Key Features:Maintained the order of elements in which they are added.Allows
15+ min read
AbstractList in Java with Examples The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Rand
7 min read
ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac
9 min read
LinkedList in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure, which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and an
12 min read
Immutable List in Java ImmutableList, as suggested by the name, is a type of List which is immutable. It means that the content of the List are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown. An I
5 min read
CopyOnWriteArrayList in Java CopyOnWriteArrayList class is introduced in JDK 1.5, which implements the List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy. It is found in java.util.concurrent package. It is a data structure created to b
6 min read
Custom ArrayList in Java Before proceeding further let us quickly revise the concept of the arrays and ArrayList quickly. So in java, we have seen arrays are linear data structures providing functionality to add elements in a continuous manner in memory address space whereas ArrayList is a class belonging to the Collection
8 min read
Difference Between Synchronized ArrayList and CopyOnWriteArrayList in Java Collection As we know that the ArrayList is not synchronized, if multiple threads try to modify an ArrayList at the same time, then the final outcome will be non-deterministic. Hence synchronizing the ArrayList is a must to achieve thread safety in a multi-threaded environment. In order to make List objects we
6 min read
How to remove a SubList from a List in Java Given a list in Java, the task is to remove all the elements in the sublist whose index is between fromIndex, inclusive, and toIndex, exclusive. The range of the index is defined by the user. Example: Input list = [1, 2, 3, 4, 5, 6, 7, 8], fromIndex = 2, endIndex = 4 Output [1, 2, 5, 6, 7, 8] Input
2 min read
Randomly Select Items from a List in Java In this article, we will explore how to efficiently select an element from a list in Java. The basic approach involves generating a random index between 0 and the size of the list, and then using that index to retrieve the item. Different Ways to Select Items from a ListBelow are the different appro
3 min read