Menu



Java program for remove duplicate Element from an array


RemoveDuplicateInArray.java

public class RemoveDuplicateInArray{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50,60,70,70,80,80};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i System.out.print(arr[i]+" ");
}
}