Friday, September 19, 2025
HomeLanguagesJavaHow to Prevent the Addition of Duplicate Elements to the Java ArrayList?

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

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.

RELATED ARTICLES

Most Popular

Dominic
32303 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11841 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7058 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS