12:55 pm - Thursday July 27, 2017

Java Example Program for delete duplicate element in an array

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

public class Task {
	/**
	 * @ author:Candid @ description:Java Program to delete Duplicate elements
	 * in an array
	 */

	public static void main(String args[])
	
	{
    
			int array[] = { 10, 20, 30, 20, 40, 40, 50, 60, 70, 80 };// array of ten	
			// elements
			int size = array.length;
			System.out.println("Size before deletion: " + size);

			for (int i = 0; i < size; i++) 
			{

				for (int j = i + 1; j < size; j++) 
				{


					if (array[i] == array[j]) // checking one element with all the
// element
					{
						while (j < (size) - 1) 
						{
							array[j] = array[j + 1];// shifting the values
							j++;
						}	
						size--;
					}
				}
			}
			System.out.println("Size After deletion: " + size);

			for (int k = 0; k < size; k++) 
			{
				System.out.println(array[k]); // printing the values
			}
	}
}

Output:

Size before deletion: 10

Size After deletion: 8

10

20

30

40

50

60

70

80

 

Filed in: Sample Programs in java

Comments are closed.