A LinkedList can store the data by use of the doubly Linked list. Each element is stored as a node. The LinkedList can have duplicate elements because of each value store as a node. But there may be a situation when we want to store only unique elements in LinkedList and want to remove duplicates from linked list. We will discuss some ways that can remove duplicates from linked list.
Here is the table content of the article will we will cover this topic.
1. By use of LinkedHashSet
2. By using the manual way
3. Using Java 8 Stream.distinct()
By use of LinkedHashSet
In the Collection framework, a Set doesn’t contain duplicate elements. So we can use any class that implements the Set interface. As we know LinkedList maintains the insertion order so we should use LinkedHashSet that will maintain order as it is. So, we will remove duplicates from linked list by converting it into LinkedHashSet.
import java.util.LinkedHashSet; import java.util.LinkedList; public class RemoveDuplicate { public static void main(String[] args) { LinkedList<Integer> duplicateElements = new LinkedList<Integer>(); duplicateElements.add(1); duplicateElements.add(2); duplicateElements.add(3); duplicateElements.add(4); duplicateElements.add(1); duplicateElements.add(3); duplicateElements.add(1); System.out.println("Elements from LinkedList: "+ duplicateElements); LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>(duplicateElements); System.out.println("Elements from LinkedHashSet: "+ hashSet); } }
Output: Elements from LinkedList: [1, 2, 3, 4, 1, 3, 1]
Elements from HashSet: [1, 2, 3, 4]
By using manual way
We can remove the duplicate element from LinkedList by traversing it and check each element one by one. We can use the forEach loop to traverse the LinkedList and contains() method to check whether the element exists or not?
1. Let’s create a new LinkedList that will store only unique elements.
2. After that traverse, the LinkedList and check each element by contains() method.
3. If the element doesn’t contain in new LinkedList the add it else ignores it.
import java.util.LinkedList; public class RemoveDuplicate { public static void main(String[] args) { LinkedList<Integer> duplicateElements = new LinkedList<Integer>(); duplicateElements.add(1); duplicateElements.add(2); duplicateElements.add(3); duplicateElements.add(4); duplicateElements.add(1); duplicateElements.add(3); duplicateElements.add(1); LinkedList<Integer> uniqueElements = new LinkedList<Integer>(); for(Integer ele: duplicateElements) { if(!uniqueElements.contains(ele)) uniqueElements.add(ele); } System.out.println("Duplicate element in LinkedList: " + duplicateElements); System.out.println("Unique element in LinkedList: " + uniqueElements); } }
Output: Duplicate element in LinkedList: [1, 2, 3, 4, 1, 3, 1]
Unique element in LinkedList: [1, 2, 3, 4]
Using Java 8 Stream.distinct()
There is another way to remove duplicates from linked list. Streams are introduced in java, so we can use the distinct() method of Stream. You can read the detailed working of the distinct() method. The distinct() method works based on Stream and returns a stream that contains only a unique element. This method internally invokes the equals() method.
import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; public class RemoveDuplicate { public static void main(String[] args) { LinkedList<Integer> duplicateElements = new LinkedList<Integer>(); duplicateElements.add(1); duplicateElements.add(2); duplicateElements.add(3); duplicateElements.add(4); duplicateElements.add(1); duplicateElements.add(3); duplicateElements.add(1); List<Integer> newList = duplicateElements.stream().distinct().collect(Collectors.toList()); System.out.println("Element from LinkedList: "+ duplicateElements); System.out.println("Element from new LinkedList: "+ newList); } }
Output: Element from LinkedList: [1, 2, 3, 4, 1, 3, 1]
Element from new LinkedList: [1, 2, 3, 4]