How to remove duplicates from a list in Java
In Java programming, it is necessary to eliminate duplicate elements from a list to prevent data repetition and code-related problems. Various techniques are available for removing duplicates from a list, including the following:
Making iterations in the list
Using the
stream.distinct ()methodUsing a
LinkedHashset
We will implement the three techniques separately to remove duplicates from the ArrayList, which is defined as:(Arrays.asList(14, 10, 14, 72, 72, 13, 13, 10, 13, 42, 56, 56));
Making iterations in the list
This method makes iterations in the array to get rid of the duplicate. However, it is not time-efficient when it comes to handling large data sets.
Below is the code implementation.
import java.util.*;public class main {public static <T> ArrayList<T> removeDupe(ArrayList<T> list){ArrayList<T> newList = new ArrayList<T>();for (T element : list) {if (!newList.contains(element)) {newList.add(element);}}return newList;}public static void main(String args[]){ArrayList<Integer> list = new ArrayList<>(Arrays.asList(14, 10, 14, 72, 72, 13, 13, 10, 13, 42, 56, 56));System.out.println("ArrayList with duplicates: "+ list);ArrayList<Integer> newList = removeDupe(list);System.out.println("ArrayList with duplicates removed: "+ newList);}}
Let’s take a look at the code explanation above:
Line 1: We import all the classes in the
java.utilpackage, which contains utility classes likeArrayListandArrays.Line 2: We define the main class of the program, which is called
main. This is where the program begins execution.Line 3: We define a generic method
removeDupe()that takes anArrayListof typeTas an argument and returns anArrayListof typeT. The method will remove all duplicate elements from the input list and return a new list without duplicates.Line 4: We create a
new ArrayListcallednewListthat will hold the unique elements.Line 5: This starts a
forloop that iterates through each element of the input list.Lines 6–7: We check if the element is already in the
newListusing thecontains()method of theArrayList. If the element is not in thenewList, it is added to thenewListusing theadd()method.Line 10: This returns the
newListwith all the duplicates removed.Line 12: We define the
mainmethod of the program in this line, which is called when the program is executed.Line 13: We create a
new ArrayListcalledlistand initialize it with a list of integers containing duplicates.Line 15: We print out the original
ArrayListthat had duplicate elements.Line 16: We call the
removeDupe ()method to remove duplicates from the list and assign the new list to anew ArrayListcallednewList.Line 17: This prints out the
new ArrayListwith duplicates removed.
Using the stream.distinct() method
In this section, we employ the distinct() operation on the initial list to generate a fresh stream of unique elements. By distinct elements, we mean those elements from the array that appear only once. This means that duplicate elements from the original list are eliminated, resulting in a stream exclusively containing distinct elements.
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class main {public static void main(String args[]) {List<Integer> withDupes = Arrays.asList(14, 10, 14, 72, 72, 13, 13, 10, 13, 42, 56, 56);System.out.println(" List with duplicates: " + withDupes);List<Integer> withoutDupes = withDupes.stream().distinct() .collect(Collectors.toList());System.out.println("\n List without duplicates: " + withoutDupes);}}
Explanation
Lines 1–3: We import all we need to work with lists, arrays and the
stream.distinct()method.Line 4: We call the
main()method of themainclass.Line 5: In this line, we use the following keywords:
public: This is an access modifier.static: This means the method can be called without an instance of the classvoid, which is the return type of the method.main: This is the name of the method.String args[]: This is the parameter of the method.
Line 6: We create a new
ListcalledwithDupesand initialize it with a set of integer values that include duplicates using theArrays.asList()method.Line 7: Here, we print out the original List using the
println()method.Line 8: We first create a new
ListcalledwithoutDupes. Then use thedistinct()method to remove any duplicates in the initial list,withDupes. The resultingListis collected using thecollect() methodand is added to thewithoutDupesArrayListusing thetoList()method.Line 9: Finally, we print out the
withoutDupeslist using theprintln()method. The\ncharacter adds a new line before the output.
Using the LinkedHashSet on integers
In this method, we utilize sets to get rid of duplicates. As we know, sets do not have room for duplicate elements. All that happens is that we convert our array into a set, a step that removes the duplicates, and then we convert it back to an array.
Below is a code sample.
import java.util.ArrayList;import java.util.Arrays;import java.util.LinkedHashSet;import java.util.Set;class main {public static void main(String[] args) {ArrayList<Integer> numbers;numbers = new ArrayList<>(Arrays.asList(14, 10, 14, 72, 72, 13, 13, 10, 13, 42, 56, 56));System.out.println("ArrayList with duplicate elements: " + numbers);Set<Integer> set = new LinkedHashSet<>();set.addAll(numbers);numbers.clear();numbers.addAll(set);System.out.println("\n ArrayList without duplicate elements: " + numbers);}}
Explanation
Lines 1–4: We import all we need to work with
Arrays,ArrayLists,Sets, andLinkedHashset.Line 5: We call the
main()method of themainclass.Line 6: In this line, we use the keywords:
public: This is an access modifier.static: This means the method can be called without an instance of the classvoid, which is the return type of the method.main: This is the name of the method.String args[]: This is the parameter of the method.
Line 7: Here,
ArrayList <Integer> numbersmeans we declare a newArrayListof typeIntegerwith the name ofnumbers.Line 8: We initialize the
numbers ArrayListwith a list of integers using theArrays.asList()method.Line 9: We print the
numbers ArrayListthat contains duplicate elements.Line 10: We create a
new LinkedHashSetnamedsetthat storesIntegerobjects.Line 11: We then add all the elements of
numbers ArrayListto the setLinkedHashSet. This will remove any duplicates from the set.Line 12: In this line, we clear all the contents of
numbers ArrayList.Line 13: We then add all the elements from set
LinkedHashSetback to thenumbers ArrayList. This will add only the unique elements from the originalnumbers ArrayList.Line 14: Finally, we print the
numbers ArrayListthat now contains only unique elements.