You are on page 1of 2

// 10.

WAP to remove duplicate from an ordered array


import java.io.*;
public class array10
{
public static void main(String arg[]) throws IOException
{
BufferedReader Br=new BufferedReader(new InputStreamReader(Syste
m.in));
duplicate d=new duplicate();
int n;
int a[]=new int[10];
String str;
System.out.println("This is a program to remove duplicate item f
rom an ordered array");
System.out.println("--------------------------------------------
--------------------");
System.out.print("Enter the size of the array: ");
str=Br.readLine();
n=Integer.parseInt(str); // taking maximum size of the ar
ray from user
System.out.println("Enter the array: ");
for(int i=0;i<n;i++)
{
System.out.print("a["+i+"]= "); // Input of array fro
m user
str=Br.readLine();
a[i]=Integer.parseInt(str);
}
System.out.println("Array after removing duplicate items is: ");
d.duplicate(a,n); // calling method duplicate
}
}
class duplicate
{
void duplicate(int a[],int n) // method to remove duplicate items fro
m array
{
for(int i=0;i<n;i++)
{
if(a[i]>=0)
System.out.println(a[i]);
for(int j=i+1;j<n;j++)
{
if(a[j]==a[i])
{
a[j]=-1;
}
}
}
}
}
/*
Output
******
This is a program to remove duplicate item from an ordered array
----------------------------------------------------------------
Enter the size of the array: 5
Enter the array:
a[0]= 1
a[1]= 2
a[2]= 1
a[3]= 3
a[4]= 4
Array after removing duplicate items is:
1
2
3
4
*/

You might also like