Published: 30 Apr 2025 | Reading Time: 8 min read
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.
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.
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.
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.
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 + " ");
}
}
}
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.
1 2 3 4 5
Pros:
Cons:
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.
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 + " ");
}
}
}
1 2 3 4 5
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:
Cons:
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.
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 + " ");
}
}
}
1 2 3 4 5
Pros:
Cons:
The Stream API is a great choice for many common scenarios, especially when simplicity and readability are prioritized over extreme performance optimizations.
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.
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 + " ");
}
}
}
1 2 3 4 5
Pros:
Cons:
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.
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] + " ");
}
}
}
1 2 3 4 5
Pros:
Cons:
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.
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.
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.
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.
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.
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.
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.
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.