Friday, February 10, 2012

Java program To remove duplicates from an array of integers so that each number may appear only once

import java.io.*;
class remDup
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of the array: ");
int l=Integer.parseInt(br.readLine());
int[]a=new int[l];
for(int i=0; i {
System.out.print("Enter the element number "+(i+1)+" of the array: ");
a[i]=Integer.parseInt(br.readLine());
}

//now the real work begins

for(int i=0; i {
for(int j=i+1; j {
while(a[i]==a[j])
{
for(int A=j; A a[A]=a[A+1];
l--;
}
}
}

// real work is done now!!!!


for(int i=0; i {
System.out.println(a[i]);
}

}// end of main
}// end of class

1 comment:

  1. You can also check here
    http://javaarcade.blogspot.in/2013/10/program-to-remove-duplicates-from-array_9265.html

    ReplyDelete