Java Program to Delete Array Duplicates

Write a Java Program to delete Array Duplicates with an example. Or how to write a Java Program to find and remove the duplicate items in a given array.

In this Java delete duplicate array number example, we used while loop to iterate Dup_Count_arrr array. Then, delete duplicate items (values shown more than once) and print the final array (without duplicates).

package ArrayPrograms;

public class DeleteArrayDuplicates1 {
	
	public static void main(String[] args) {
		
		int i = 0, j, k;
		int[] Dup_Count_arr = {10, 15, 25, 10, 8, 12, 10, 15, 55, 10, 60};
		
		int Size = Dup_Count_arr.length - 1;
		
		while(i < Size) 
		{
			j = i + 1;
			while(j < Size)
			{		
				if(Dup_Count_arr[i] == Dup_Count_arr[j]) {
					k = j;
					while(k < Size) {
						Dup_Count_arr[k] = Dup_Count_arr[k + 1];
						k++;
					}
					Size--;
					j--;
				}
				j++;
			}
			i++;
		}
		System.out.print("\nThe Final Array after Deleting Duplicates  = " );
		for(i = 0; i < Size; i++) 
		{
			System.out.format("%d ", Dup_Count_arr[i]);
		}
	}
}
The Final Array after Deleting Duplicates  = 10 15 25 8 12 55 

Java Program to Delete Duplicate Items in an Array using For Loop

package ArrayPrograms;

import java.util.Scanner;

public class DeleteArrayDuplicates2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, j, k;
		int[] Del_Dup_arr = new int[50];
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the Duplicate Array size  : ");
		Size = sc.nextInt();
		
		System.out.format("\nEnter Duplicate Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			Del_Dup_arr[i] = sc.nextInt();
		}
		
		for(i = 0; i < Size; i++) 
		{
			for(j = i + 1; j < Size; j++)
			{
				if(Del_Dup_arr[i] == Del_Dup_arr[j]) {
					for(k = j; k < Size; k++) {
						Del_Dup_arr[k] = Del_Dup_arr[k + 1];
					}
					Size--;
					j--;
				}
			}
		}
		System.out.print("\nThe Final Array after Deleting Duplicates  = " );
		for(i = 0; i < Size; i++) 
		{
			System.out.format("%d ", Del_Dup_arr[i]);
		}
	}
}
Java Program to Delete Duplicate array items 1

In this Java code for remove duplicate array items example, we created a separate function DeleteDuplicateArrayItems to find and delete duplicate values and the prints the final array.

package ArrayPrograms;

import java.util.Scanner;

public class DeleteArrayDuplicates3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i;
		int[] Del_Dup_arr = new int[50];
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the Duplicate Array size  : ");
		Size = sc.nextInt();
		
		System.out.format("\nEnter Duplicate Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			Del_Dup_arr[i] = sc.nextInt();
		}		
		DeleteDuplicateArrayItems(Del_Dup_arr, Size);
	}
	public static void DeleteDuplicateArrayItems(int[] Del_Dup_arr, int Size) {
		int i, j, count = 0;
		int[] x = new int[50];
		
		for(i = 0; i < Size; i++) 
		{
			for(j = 0; j < count; j++)
			{
				if(Del_Dup_arr[i] == x[j]) {
					break;
				}
			}
			if(j == count) {
				x[count] = Del_Dup_arr[i];
				count++;
			}			
		}
		System.out.print("\nThe Final Array after Deleting Duplicates  = " );
		PrintArrayElement(x, count);
		
	}
	public static void PrintArrayElement(int[] arr, int size) {
		int i;
		
		for(i = 0; i < size; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}

Java Delete Duplicate Array Items using a For Loop and functions output

Please Enter the Duplicate Array size  : 12

Enter Duplicate Array 12 elements : 10 20 30 10 40 10 50 20 60 40 70 30 

The Final Array after Deleting Duplicates  = 10 20 30 40 50 60 70