In various data handling scenarios in programming, there arises a need to remove duplicates from the containers i.e., Array. It is done to enhance the code readability and manage the memory effectively. To address this query, Java provides a built in container and other alternative algorithm approaches that assist the developer to a great extent.
How to Remove Duplicates From an Array in Java?
To omit duplicates from an array, consider the following approaches:
- Using “HashSet”.
- Using a Separate Index.
- Using a Separate Array.
Approach 1: Remove Duplicates From an Array Using HashSet
The most effective approach to remove duplicates from an array in Java is via “HashSet” since the Set don’t store the duplicate values:
import java.util.*;
public class Removeduplicates {
public static void main (String[] args) {
int givenArray[] = {1, 2, 2, 3, 3, 4, 5};
int n = givenArray.length;
HashSet x = new HashSet();
for (int i= 0; i<n; i++)
x.add(givenArray[i]);
System.out.print(x);
}}
In this block of code:
- Import the library required to access HashSet.
- In the next step, create an integer array and compute its length via the “length” property.
- Now, create a HashSet object, iterate the array based on its length via the “for” loop and append the array elements in the HashSet.
- The HashSet automatically removes the duplication found in the values.
Output

This outcome implies that the duplication is removed from the array elements successfully.
Approach 2: Remove Duplicates From an Array Using a Separate Index
In this code example, the duplicate values from an array can be removed by iterating the passed array and storing the elements in a separate index via a user-defined function:
public class Removeduplicates {
public static int omitDuplicates(int array[], int arrayLength){
if (arrayLength == 0 || arrayLength == 1) {
return arrayLength;
}
int j = 0;
for (int i = 0; i < arrayLength - 1; i++) {
if (array[i] != array[i + 1]) {
array[j++] = array[i];
}}
array[j++] = array[arrayLength - 1];
return j;
}
public static void main(String[] args){
int array[] = {10, 10, 20, 20, 30};
int arrayLength = array.length;
int j=0;
j = omitDuplicates(array, arrayLength);
for (int i = 0; i < j; i++)
System.out.print(array[i] + " ");
}}
In this snippet of code, perform the below-given steps:
- Define a function named “omitDuplicates()”. The specified first parameter refers to the passed array and the second parameter corresponds to its length.
- In its definition, the “if” condition specifies that if the arrays’ length is 0 or 1, it is already sorted.
- In the next step, initialize the value of “j” to “0” that refers to a separate index.
- Now, iterate the passed array such that if the iterated element is not equal to its next element, it is appended at the jth index in the same array.
- This implies that the target element will only be added once in the array
- In “main”, initialize the array to be passed for evaluation.
- Also, compute its length via the “length” property.
- Lastly, invoke the defined function by passing the array and its length as its arguments, respectively and use the “for” loop to print the array elements.
Output

From here, it can be verified that the duplication in the array elements is removed appropriately.
Approach 3: Remove Duplicates From an Array Using a Separate Array
In this particular approach, the duplication of elements from an array will be omitted by storing the unique elements in a separate array:
public class Removeduplicates {
public static int omitDuplicates(int array[], int arrayLength){
if (arrayLength == 0 || arrayLength == 1) {
return arrayLength;
}
int[] newArray = new int[arrayLength];
int j = 0;
for (int i = 0; i < arrayLength - 1; i++) {
if (array[i] != array[i + 1]) {
newArray[j++] = array[i];
}}
newArray[j++] = array[arrayLength - 1];
for (int i = 0; i < j; i++) { array[i] = newArray[i];
}
return j;
}
public static void main(String[] args){
int array[] = {10, 10, 15, 15, 20};
int arrayLength = array.length;
arrayLength = omitDuplicates(array, arrayLength);
for (int i = 0; i < arrayLength; i++)
System.out.print(array[i] + " ");
}}
According to these code lines:
- Recall the discussed approaches for defining a function and analyzing the array’s length.
- After that, create an integer array for containing the unique elements that equals the size of the passed array.
- Now, iterate the passed array and copy all the unique elements from the passed array to the newly created array. Also, maintain count of unique elements via “j”.
- Apply the “for” loop to iterate along the unique elements placed in the newly created integer array and store them in the passed array again.
- In “main” repeat the discussed methodology for creating and passing the array and its length as the function’s arguments and display the array.
Output

Conclusion
To remove duplicates from an array in Java, utilize a “HashSet”, a separate index, or a separate array. The HashSet approach is the most effective as it removes the duplicates automatically without any sort of algorithm.