Finding Duplicate element using hashset

package checkduplicate;
import java.util.*;
public class CheckdupliHash {

public static void main(String args[])
{
ArrayList<Integer> al=new ArrayList<Integer>();
al=addNumbers(al);
System.out.println(“Duplicacy”+checkdup(al));
}
static ArrayList<Integer> addNumbers(ArrayList<Integer> al)
{
int ch,num;
while(true)
{
System.out.println(“select choice 1.to add number 2.to stop entering”);
Scanner sc=new Scanner(System.in);
ch=sc.nextInt();
switch(ch)
{
case 1:System.out.println(“Please Enter the Number”);
num=sc.nextInt();
al.add(num);
break;
case 2:System.out.println(“Entering numbers is not allowed”);
return al;

default:System.out.println(“Selected wrong choice”);
break;
}
}
}
static boolean checkdup(ArrayList<Integer> al)
{ HashSet<Integer> hm=new HashSet<Integer>();
for(int i=0;i<al.size();i++)
if(hm.contains(al.get(i)))
return true;
else
hm.add(al.get(i));
return false;

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *