Java Program to Delete Array Duplicates

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

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

public class DelArrDupes1 {
	
	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 Array Duplicates items 1

Java Program to Delete Array Duplicates using functions

In this example code, we created a separate function DelDupArItems to find and delete duplicate values and print the final array.

import java.util.Scanner;

public class DelArDupes3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Sz, i;
		int[] Del_Dup_arr = new int[50];
		
		sc = new Scanner(System.in);
		
		System.out.print("\nPlease Enter the size  : ");
		Sz = sc.nextInt();
		
		System.out.format("\nEnter %d elements : ", Sz);
		for(i = 0; i < Sz; i++) 
		{
			Del_Dup_arr[i] = sc.nextInt();
		}		
		DelDupArItems(Del_Dup_arr, Sz);
	}
	public static void DelDupArItems(int[] Del_Dup_arr, int Sz) {
		int i, j, count = 0;
		int[] x = new int[50];
		
		for(i = 0; i < Sz; 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  = " );
		PrintArElem(x, count);
		
	}
	public static void PrintArElem(int[] arr, int sz) {
		int i;
		
		for(i = 0; i < sz; i++) 
		{
			System.out.format("%d ", arr[i]);
		}
	}
}
Please Enter the size  : 12

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

The Final Array  = 10 20 30 40 50 60 70 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.