Java Program to Remove Duplicates From Array

You are currently viewing Java Program to Remove Duplicates From Array

Here, I’m going to share the information about Java Program to Remove Duplicates From Array. Initially, We will talk about duplicate elements in the array and how to do a task using this program?

The repeated elements or data types in the array are called duplicate elements in the array. We can remove the duplicate elements in an array only if an array is sorted in order. Let’s see the possibilities below.

Java Program to Remove Duplicates From Array

This is an example program to execute a task here.

import java.util.Arrays;
 
public class Exercise16 {
   static void unique_array(int[] my_array)
    {
        System.out.println("Original Array : ");
         
        for (int i = 0; i < my_array.length; i++)
        {
            System.out.print(my_array[i]+"\t");
        }
         
        //Assuming all elements in input array are unique
         
        int no_unique_elements = my_array.length;
         
        //Comparing each element with all other elements
         
        for (int i = 0; i < no_unique_elements; i++) 
        {
            for (int j = i+1; j < no_unique_elements; j++)
            {
                //If any two elements are found equal
                 
                if(my_array[i] == my_array[j])
                {
                    //Replace duplicate element with last unique element
                     
                    my_array[j] = my_array[no_unique_elements-1];
                     
                    no_unique_elements--;
                     
                    j--;
                }
            }
        }
         
        //Copying only unique elements of my_array into array1
         
        int[] array1 = Arrays.copyOf(my_array, no_unique_elements);
         
        //Printing arrayWithoutDuplicates
         
        System.out.println();
         
        System.out.println("Array with unique values : ");
         
        for (int i = 0; i < array1.length; i++)
        {
            System.out.print(array1[i]+"\t");
        }
         
        System.out.println();
         
        System.out.println("---------------------------");
    }
     
    public static void main(String[] args) 
    {        
        unique_array(new int[] {0, 3, -2, 4, 3, 2});
         
        unique_array(new int[] {10, 22, 10, 20, 11, 22});
         
      }    
}

Source Code Execution

After importing the java.util.Arrays() class into this program, We have to declare the two input arrays with variables. The for loop will iterate from 1st element to the last element of arrays to check if there are any duplicate element repeats or not.

If there are any repeated elements found then it is removed and the new array’s output will be stored in the new variables. Finally, We will get and see the removed duplicate elements output on the screen using the system.out.printIn() function.

Read Also : JavaScript Program to Remove Duplicates From Array

Output of Java Program to Remove Duplicates From Array

Below, You will see the reference output of the Java Program to Remove Duplicates From Array.

Original Array : 0	3	-2	4	3	2	
Array with unique values : 
0	3	-2	4	2---------------------------
Original Array : 
10	22	10	20	11	22	
Array with unique values : 
10	22	11	20	
---------------------------
	

Final Words

We hope this article, Java Program to Remove Duplicates From Array will be useful to you. If you have any queries regarding this program let me know via the comment section. Share the article with other Java learners on social media. Give your powerful feedback about this topic with us.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply