How to Prevent the Addition of Duplicate Elements to the Java ArrayList? - GeeksforGeeks

11 Dec, 2020
Ever wondered how you can make an ArrayList unique? Well, in this article we’ll be seeing how to prevent the addition of duplicates into our ArrayList. If an ArrayList have three duplicate elements, but at the end, only the ones which are unique are taken into the ArrayList and the repetitions are neglected can be done using various approaches discussed as below.
Example:
Input : [1, 1, 2, 2, 3, 3, 4, 5, 8]
Output: [1, 2, 3, 4, 5, 8]

Input : [1, 1, 1, 1, 1, 1, 1, 1, 1]
Output: [1]
Approach 1: contains() method
Add elements one by one.Check for their presence using the contains method.Ignore the current element if it returns true.Else add the element.
Add elements one by one.
Check for their presence using the contains method.
Ignore the current element if it returns true.
Else add the element.
Below is the implementation of the above approach:
Java
// Java Program to prevent the addition// of duplicate elements to an ArrayList.  // Importing the ArrayList classimport java.util.ArrayList;  class GFG {    public static void main(String[] args)    {          // Input        int array[] = { 1, 1, 2, 2, 3, 3, 4, 5, 8 };          // Creating an empty ArrayList        ArrayList<Integer> ans = new ArrayList<>();          for (int i : array) {              // Checking if the element is already present or            // not            if (!ans.contains(i)) {                // Adding the element to the ArrayList if it                // is not present                ans.add(i);            }        }          // Printing the elements of the ArrayList        for (int i : ans) {            System.out.print(i + " ");        }    }}
1 2 3 4 5 8
Time Complexity: O(N2), as contains method can traverse through the entire array in the worst case.
Space Complexity: O(1), as no extra space is used.
 
Approach 2: HashSet
Add elements one by one.Check for their presence using HashSet.Ignore the current element if it returns true.Else add the element.
Add elements one by one.
Check for their presence using HashSet.
Ignore the current element if it returns true.
Else add the element.
 Below is the implementation of the above approach:
Java
// Java Program to prevent the addition// of duplicate elements to an ArrayList.  // Importing the ArrayList classimport java.util.ArrayList;  // Importing the HashSet classimport java.util.HashSet;  class GFG {    public static void main(String[] args)    {          // Input        int array[] = { 1, 1, 1, 1, 1, 1, 1, 1 };          // Creating an empty ArrayList        ArrayList<Integer> ans = new ArrayList<>();          // Creating an empty HashSet        HashSet<Integer> set = new HashSet<>();          for (int i : array) {              // Checking if the element is already present or            // not            if (!set.contains(i)) {                // Adding the element to the ArrayList if it                // is not present                ans.add(i);                // Adding the element to the HashSet if it                // is not present                set.add(i);            }        }          // Printing the elements of the ArrayList        for (int i : ans) {            System.out.print(i + " ");        }    }}
1
Time Complexity: O(n)
Space Complexity: O(n), as a HashSet is used to store the traversed elements.
Java-ArrayList
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java