Remove Duplicates from Array Java: A Complete Guide

Published: 30 Apr 2025 | Reading Time: 8 min read

Overview

Removing duplicates from an array is a common task that programmers often need to tackle. There are various methods to remove duplicates from array in Java, and the best approach can depend on whether the array is sorted or unsorted.

In this guide, you will explore multiple techniques to remove duplicates from an array in Java. We will cover practical examples, discuss the advantages and disadvantages of each method, and help you choose the most efficient approach based on your needs.

Table of Contents

Remove Duplicates from Array in Java

Arrays are fundamental data structures in Java, used to store collections of elements of the same type. However, when working with arrays, it's necessary to remove duplicate elements to ensure data integrity or optimize storage and processing. Java provides several approaches to remove duplicates, each with its strengths and weaknesses.

From traditional loops to modern data structures like HashSet and Stream API, developers have multiple options to eliminate duplicate values. The choice of method depends on factors such as performance requirements, array size, and whether the array is sorted or unsorted.

Methods for Removing Duplicates in Java

There are multiple ways to remove duplicates from array in Java. The choice of method depends on factors such as whether the array is sorted or unsorted, performance considerations, and whether we need to preserve the original order of elements.

1. Using ArrayList

One simple method to remove duplicates while preserving the original order is by using an ArrayList. This approach involves iterating through the array and adding each element to the ArrayList only if it doesn't already exist.

Steps to Implement

  1. Create an ArrayList to store unique elements.
  2. Iterate through the array.
  3. Add elements to the ArrayList if they are not already present.
  4. Convert the ArrayList back to an array.

Example Code to Remove Duplicates from Array in Java Using ArrayList

import java.util.ArrayList;
import java.util.List;

public class RemoveDuplicates {
    public static int[] removeDuplicatesWithList(int[] data) {
        List<Integer> uniqueElements = new ArrayList<>();

        for (int value : data) {
            if (!uniqueElements.contains(value)) {  // Check for uniqueness
                uniqueElements.add(value);
            }
        }

        return uniqueElements.stream().mapToInt(i -> i).toArray();  // Convert back to array
    }

    public static void main(String[] args) {
        int[] data = {1, 2, 3, 2, 4, 1, 5};
        int[] uniqueData = removeDuplicatesWithList(data);

        for (int num : uniqueData) {
            System.out.print(num + " ");
        }
    }
}

Explanation of the Code

The program removes duplicates from an integer array using an ArrayList. It iterates through the given array and adds each element to the ArrayList only if it is not already present. Since ArrayList.contains(value) checks for existing elements in O(n) time, this approach results in an overall time complexity of O(n²), making it inefficient for large arrays.

After collecting unique elements, the program converts the ArrayList into an integer array using Java Streams (stream().mapToInt(i -> i).toArray()). The main method initializes an integer array {1, 2, 3, 2, 4, 1, 5}, removes duplicates using removeDuplicatesWithList(), and prints the resulting array, maintaining the original order of elements.

Output

1 2 3 4 5

Pros and Cons

Pros:

Cons:

2. Using LinkedHashSet to Remove Duplicates in Java

LinkedHashSet is part of Java's java.util package and is a hybrid data structure that combines the properties of a HashSet and a LinkedList. It automatically removes duplicate values while maintaining the insertion order of elements, making it a better alternative to HashSet, which does not guarantee order.

Steps to Implement

  1. Create a LinkedHashSet – This will store unique elements while preserving their order.
  2. Iterate through the array and add elements to LinkedHashSet – Since LinkedHashSet does not allow duplicates, repeated elements will automatically be ignored.
  3. Convert the LinkedHashSet back to an array – Use Java Streams to transform the LinkedHashSet into an int[] array.

Example Code to Remove Duplicates from Array in Java Using LinkedHashSet

import java.util.LinkedHashSet;
import java.util.Set;

public class RemoveDuplicates {
    public static int[] removeDuplicatesWithSet(int[] data) {
        Set<Integer> uniqueElements = new LinkedHashSet<>(); // Maintains order while removing duplicates

        for (int value : data) {
            uniqueElements.add(value);  // Only unique elements get stored
        }

        return uniqueElements.stream().mapToInt(i -> i).toArray();  // Convert back to an int array
    }

    public static void main(String[] args) {
        int[] data = {1, 2, 3, 2, 4, 1, 5};
        int[] uniqueData = removeDuplicatesWithSet(data);

        for (int num : uniqueData) {
            System.out.print(num + " ");
        }
    }
}

Output

1 2 3 4 5

Explanation of the Code

When to Use LinkedHashSet?

This approach is a great balance between efficiency and maintaining order, making it suitable for most real-world applications where duplicates need to be removed from lists while keeping the sequence intact.

Pros and Cons

Pros:

Cons:

3. Using Java Stream API to Remove Duplicates

The Stream API, introduced in Java 8, offers a functional approach to Remove Duplicates from Array Java. It provides a clean and concise way to perform various operations, including filtering out duplicates from arrays. Using the distinct() method in the Stream API can efficiently remove duplicates, while retaining the order of elements.

Steps to Implement

  1. Create a Stream from the Array: Use Arrays.stream(data) to convert the input array into a stream of elements.
  2. Apply distinct() to Remove Duplicates: The distinct() method automatically filters out duplicates by comparing elements using their equals() method.
  3. Convert the Stream Back to an Array: The toArray() method is used to collect the distinct elements back into an array.

Example Code to Remove Duplicates from Array in Java Using Java Stream API

import java.util.Arrays;

public class RemoveDuplicates {
    public static int[] removeDuplicatesWithStreams(int[] data) {
        return Arrays.stream(data)      // Convert the array to a stream
                     .distinct()         // Remove duplicates
                     .toArray();        // Convert back to array
    }

    public static void main(String[] args) {
        int[] data = {1, 2, 3, 2, 4, 1, 5};
        int[] uniqueData = removeDuplicatesWithStreams(data);

        for (int num : uniqueData) {
            System.out.print(num + " ");
        }
    }
}

Explanation of the Code

Output

1 2 3 4 5

Pros and Cons

Pros:

Cons:

When to Use the Java Stream API for Removing Duplicates?

The Stream API is a great choice for many common scenarios, especially when simplicity and readability are prioritized over extreme performance optimizations.

4. Using HashMap to Remove Duplicates

A HashMap is a data structure in Java that stores key-value pairs, where each key is unique. You can use a HashMap to remove duplicates from array Java efficiently by storing the elements as keys in the map. Since keys in a HashMap are unique, any duplicate elements will be ignored when added to the map. This approach is both time-efficient and space-efficient in most cases.

Steps to Implement

  1. Create a HashMap: Use a HashMap where the key will be the array element and the value will be a placeholder (true in this case). The value is irrelevant because we only care about the uniqueness of the keys.
  2. Add elements to the HashMap: Iterate over the input array, and for each element, add it to the HashMap using the put() method. The HashMap ensures that only unique keys are stored.
  3. Retrieve unique elements: After processing the array, you can use keySet() to get the unique keys and convert them into an array.
  4. Return the result: Convert the set of unique keys into an array using Java Streams.

Example Code to Remove Duplicates from Array in Java Using HashMap

import java.util.HashMap;
import java.util.Map;

public class RemoveDuplicates {
    public static int[] removeDuplicatesWithMap(int[] data) {
        Map<Integer, Boolean> uniqueElements = new HashMap<>();

        // Add elements to the HashMap, only unique keys will be stored
        for (int value : data) {
            uniqueElements.put(value, true);  // The value is irrelevant, only the key is stored
        }

        // Convert the keySet to an array and return
        return uniqueElements.keySet().stream().mapToInt(i -> i).toArray();
    }

    public static void main(String[] args) {
        int[] data = {1, 2, 3, 2, 4, 1, 5};
        int[] uniqueData = removeDuplicatesWithMap(data);

        for (int num : uniqueData) {
            System.out.print(num + " ");
        }
    }
}

Explanation of the Code

Output

1 2 3 4 5

Pros and Cons

Pros:

Cons:

5. In-Place Removal (For Sorted Arrays)

When dealing with sorted arrays, duplicates can be efficiently removed in place without using extra memory for auxiliary data structures. The main advantage of this approach is that it allows the array to be modified directly, which saves both time and space compared to other methods that require additional collections or arrays.

Steps to Implement

  1. Check for Empty Array: If the input array is empty, return 0 as there are no elements to process.
  2. Use Two Pointers: One pointer (i) keeps track of the position for the next unique element. The other pointer (j) iterates through the array to check each element.
  3. Compare Elements: If the current element (nums[j]) is different from the previous unique element (nums[i]), increment i and move the current element to nums[i].
  4. Return the Length: After all unique elements have been moved to the front, return i + 1, which is the count of unique elements in the array.

Example Code to Remove Duplicates from Array in Java Using In-Place Removal

public class RemoveDuplicates {
    public int removeDuplicatesInPlace(int[] nums) {
        // Check if the array is empty
        if (nums.length == 0) return 0;

        int i = 0;  // Pointer for the last unique element

        // Iterate through the array starting from index 1
        for (int j = 1; j < nums.length; j++) {
            if (nums[j] != nums[i]) {  // Found a unique element
                i++;  // Move the unique element pointer
                nums[i] = nums[j];  // Move unique element to the front
            }
        }

        // Return the number of unique elements
        return i + 1;
    }

    public static void main(String[] args) {
        int[] nums = {1, 1, 2, 3, 3, 4, 5, 5};
        RemoveDuplicates remover = new RemoveDuplicates();
        int uniqueLength = remover.removeDuplicatesInPlace(nums);

        // Print the unique elements
        for (int i = 0; i < uniqueLength; i++) {
            System.out.print(nums[i] + " ");
        }
    }
}

Explanation of the Code

Output

1 2 3 4 5

Pros and Cons

Pros:

Cons:

Conclusion

Remove duplicates from array Java is a common task, and there are several efficient methods available in Java, each with its strengths and limitations. For simple cases, ArrayList and LinkedHashSet provide easy-to-implement solutions, while HashMap is optimal for larger datasets with its O(n) time complexity.

If memory efficiency is critical, in-place removal is the best choice for sorted arrays, offering O(n) time complexity with no extra space required. The Stream API provides a concise and readable solution, but it can vary in performance. Understanding the array's properties, such as whether it's sorted or unsorted, can help you choose the best method to remove duplicates from Array in Java based on the problem.

Frequently Asked Questions

1. What is the simplest way to remove duplicates from an array in Java?

The simplest way is to use an ArrayList, where you iterate through the array and add each element to the list if it doesn't already exist. However, this method can become inefficient for larger arrays due to its O(n²) complexity.

2. What is the best method for removing duplicates from large arrays?

For large arrays, using a LinkedHashSet or a HashMap is ideal. These methods offer O(n) time complexity, making them highly efficient for large datasets.

3. Can I remove duplicates from an array without using extra memory?

Yes, for sorted arrays, you can remove duplicates in-place using a two-pointer approach. This method doesn't require extra space but is only applicable to sorted arrays.

4. What happens if I try to remove duplicates from an unsorted array?

If the array is unsorted, using methods like LinkedHashSet or HashMap will work well. If you choose the in-place approach, the array must first be sorted, which may add additional time complexity.

5. How does Java's Stream API help in removing duplicates?

Java's Stream API provides a concise, one-line solution for removing duplicates using the distinct() method. It's highly readable, but performance can vary based on the implementation and array size.

6. Why would I choose a HashMap to remove duplicates?

A HashMap allows for efficient duplicate removal with O(n) time complexity. It also works well with large datasets, though it consumes more memory than other methods.

7. Does in-place removal work for unsorted arrays?

No, in-place removal only works for sorted arrays. If the array is unsorted, you will need to either sort it first or use other methods like LinkedHashSet or HashMap for duplicate removal.

Related Articles