Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Removing Duplicate Elements In Java Array

Published on Jul 08,2019 240 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

No matter what programming language you work on. While working with arrays you are bound to come across data and especially duplicates, that you would be wanting to get rid off. This article will help you in removing data in Java array. We would take a look a three different methods for removing duplicate elements in java array.

So let us get started then,

Removing Duplicate Elements In Java Array

To remove the duplicate elements present in an array and get a unique array, we use multiple methods and procedures. The most important ones are given below:

Method 1

In this method, we remove the duplicate elements by using a temporary array.


public class Main{
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 the original array
for(int i=0; i<j; i++){
array[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int array[] = {18,18,25,25,25,28,28,29};
int length = array.length;
length = removeDuplicates(array, length);
//Printing The array elements
for(int i=0; i<length; i++)
System.out.print(array[i]+" ");

}

}

We create a temporary array to store the unique elements. The initial array is traversed, and the unique elements are copied to the temporary array. Track of count of the unique element is kept using “j”. The value present in j is then copied from the temporary array to the initial array, after which j is returned.

The program will remove all duplicate elements present in the array.

Output:

18,25,28,29

Let us continue with this article on ‘Removing duplicate elements in Java array’

Method 2: Removing Duplicate Elements In Java Array

In this method, a separate index is used.


public class Main{
public static int removeDuplicates(int array[], int n){
if(n==0 || n==1){
return n;
}
int j = 0;//for next element
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[] = {18,18,25,25,25,28,28,29};
int length = array.length;
length = removeDuplicates(array, length);
//printing array elements
for(int i=0; i<length; i++)
System.out.print(array[i]+" ");
}
}

18,25,28,29

Let us move to the final bit of this article,

Method 3

Here, we remove the duplicate elements in an unsorted array. The unsorted array is first sorted using the Array.sort() method.


import java.util.Arrays;
public class Main{
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]+" ");
}
}

Output:

18,25,28,29

The methods mentioned in this article prevent the duplicity of elements in a java array.

Thus we have come to an end of this article on ‘Removing duplicate elements in Java Array’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.