Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. Add elements one by one.
  2. Check for their presence using the contains method.
  3. Ignore the current element if it returns true.
  4. 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 class
import 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 + " ");
        }
    }
}

                    

Output
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

  1. Add elements one by one.
  2. Check for their presence using HashSet.
  3. Ignore the current element if it returns true.
  4. 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 class
import java.util.ArrayList;
  
// Importing the HashSet class
import 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 + " ");
        }
    }
}

                    

Output
1

Time Complexity: O(n)

Space Complexity: O(n), as a HashSet is used to store the traversed elements.



Last Updated : 11 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads