Saturday, 19 September 2015

Remove Duplicate Elements from Array


Enter a Array Size:
5
Enter a Array Value:
2
1
3
2
3
Array without Duplicate Elements
1
2
3

=======================

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class ArrayDuplicate {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Array Size:");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];

System.out.println("Enter a Array Value:");
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(br.readLine());
}
HashSet hs=new HashSet();
for(int i=0;i<a.length;i++){
hs.add(a[i]);
}

System.out.println("Array without Duplicate Elements");
Iterator<E> it=hs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}

}

No comments:

Post a Comment