In this program, I will share how to remove duplicate element in an array using java programming language with an example/output.
Copy the below java program and execute it with the help of Java compiler to see the output. At the end of this tutorial, I have shared some execution results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class RemoveDuplicateInArray { public static int removeDuplicateElement(int arr[], 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 (arr[i] != arr[i+1]) temp[j++] = arr[i]; temp[j++] = arr[n-1]; // Changing original array for (int i=0; i<j; i++) arr[i] = temp[i]; return j; } public static void main (String[] args) { int arr[] = {60,20,60,30,40,40,50,50}; int length = arr.length; length = removeDuplicateElement(arr, length); //printing array elements for (int i=0; i<length; i++) System.out.print(arr[i]+" "); } } |
60 20 60 30 40 50
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Java program, Java programming, Java programs, Java programs with examples, Java programs with output, java tutorial, Java tutorials, remove duplicate element from array