All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Remove Duplicates From Array Java

Last Updated : Mar 11, 2024

Remove Duplicates From Array Java

In this article we will show you the solution of remove duplicates from array java, a collection of items is called an array, and it is stored at contiguous locations in memory. A collection of similar items is stored together in this case.

A simple way to understand arrays is to envision them as stairways with numbers assigned to each step.

Multiple methods and procedures are used to separate duplicate elements from an array and make it unique.

We will now discuss the idea of how to remove duplicates from arrays in java with an example.

Step By Step Guide On Remove Duplicates From Array Java :-

import java.util.Arrays;
public class TalkersCode{
public static int removeDuplicates(int array[], int n){
if(n==0 || n==1){
return 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];
//Changing original array
for(int i=0; i<j; i++){
array[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int array[] = {25,28,18,29,25,18,29,28,25,18};//unsorted array
Arrays.sort(array);//sorting array
int length = array.length;
length = removeDuplicates(array, length);
//printing array elements
for(int i=0; i<length; i++)
System.out.print(array[i]+" ");
}
}
  1. As a first step, we define a public class named "TalkersCode".
  2. As part of the class, we define a static method called "removeDuplicates" that takes an integer array with the size "n" as its input.
  3. When input size "n" is zero or one, then we return "n" as is, since duplicates are not present.
  4. By creating a new integer array of the same size as the input array "n", we create a new array called "temp".
  5. In order to keep track of the size of the array, we initialize a variable called "j" to zero.
  6. Checking for duplicates in the original array is done by running a for loop from 0 to n-1.
  7. We add the current element to the new array "temp" if it doesn't equal the next element and increment the value of "j".
  8. In order to create the new array "temp", we need to add the last element of the current array to it.
  9. To complete this process, we are going to copy the elements from the new array "temp" into the array that was created earlier.
  10. Finally, we return the size of the new array with no duplicate elements based on the value of "j".
  11. There are some duplicate elements in the main method's integer array, so we create an unsorted array named array.
  12. Arrays are sorted ascendingly by calling the "Arrays.sort" method.
  13. The "removeDuplicates" method removes any duplicates in the sorted array based on the length of the array.
  14. By using a for loop, we can print the array elements without duplicating them.

Conclusion :-

As a result, we have successfully learned how to remove duplicates from arrays in java with an example.

As part of ensuring that the elements of the linkedHashSet are unique and ensuring the order is maintained, we used linkedHashSet in the program above.

It's well known that a set cannot contain duplicates, which is why we're able to do that via this property.

I hope this article on remove duplicates from array java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪