When we deal with bulky data, one of the efficient ways to manage it is by storing it in the array data structure. With its pros, it has some cons as well. The most common problem that arises while storing data in arrays is that we get duplicate elements. There are multiple ways to remove duplicates from arrays in Java. In this article, we will get to know about each way in detail one by one. Read till the end to completely learn about how to remove duplicates from an array in Java.
Before digging into the main topic of this article, let's just quickly remember the concept of arrays. An array is a data structure that stores elements in consecutive memory spaces. We assign this memory space while declaring an array. Each memory space is important to us. So getting duplicates in an array is of no use and we must remove duplicates from the array.
There are multiple methods to remove duplicates from an array in Java. Some of them are listed below.
- Using a temporary array
- Using separate index
- Using arrays.sort() method
- Using Hashmap
- Using a linked hash set
- Using streams
Now we will discuss every method one by one in detail.
Temporary Array to remove duplicates from array in java
In this method, we will declare another array that will be a temporary array. In this temporary array, we will store our original array but without duplicate elements. After that, we will store the values of the temporary array in the original array.
Code example - Temporary array to remove duplicates from array in java
public class removeDuplicatesFromArray{
public static int removeDuplicates(int array[], int n){
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (array[i] != array[i+1]){
temp[j++] = array[i];
}
}
temp[j++] = array[n-1];
for (int i=0; i<j; i++){
array[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int array[] = {1, 2, 3, 3, 4, 6, 6, 7, 9, 9, 9, 10, 11, 11, 11};
int length = array.length;
System.out.println("Array Before Removing Duplicates:");
for (int i=0; i<length; i++)
System.out.print(array[i]+" ");
length = removeDuplicates(array, length);
System.out.println("\nArray After Removing Duplicates:");
for (int i=0; i<length; i++)
System.out.print(array[i]+" ");
}
}
Output
Array Before Removing Duplicates:
1 2 3 3 4 6 6 7 9 9 9 10 11 11 11
Array After Removing Duplicates:
1 2 3 4 6 7 9 10 11
A prerequisite for using this method is that our array must be sorted. If the array is not sorted, then you can use the arrays.sort() method to sort the array. We have used a user-defined method for removing duplicates from an array in java. The main part where the whole logic lies in the if condition.
if (array[i] != array[i+1]){
temp[j++] = array[i];
We have removed the duplicates by comparing the elements and adding unequal elements to the temporary array.
Separate index to remove duplicates from array in java
This method is quite similar to the method described above. The only difference is that instead of using a new array i.e. temporary array, we will make changes in the original array. We will introduce a new index to make changes in the existing array. There is no need of declaring a new array after introducing a new pointer.
Code example - Separate index to remove duplicates from array in java
public class removeDuplicatesFromArray{
public static int removeDuplicates(int array[], int n){
int j = 0;
for (int i=0; i<n-1; i++){
if (array[i] != array[i+1]){
array[j++] = array[i];
}
}
array[j++] = array[n-1];
return j;
}
public static void main (String[] args) {
int array[] = {1, 2, 3, 3, 4, 6, 6, 7, 9, 9, 9, 10, 11, 11, 11};
int length = array.length;
System.out.println("Array Before Removing Duplicates:");
for (int i=0; i<length; i++)
System.out.print(array[i]+" ");
length = removeDuplicates(array, length);
System.out.println("\nArray After Removing Duplicates:");
for (int i=0; i<length; i++)
System.out.print(array[i]+" ");
}
}
Output
Array Before Removing Duplicates:
1 2 3 3 4 6 6 7 9 9 9 10 11 11 11
Array After Removing Duplicates:
1 2 3 4 6 7 9 10 11
The new index j is removing the duplicates from the array.
Arrays.sort() method to remove duplicates from array in java
In the above methods, our array must be sorted. But what if our array is not sorted and we have to remove duplicates from it. We can use arrays.sort() method to sort our array first and then remove the duplicates from the array in java.
Join the conversation! Your thoughts help the community grow.