I am just trying a simple program on finding duplicates in given array.
/* Using HashSet */
int[] arrays = {1,2,4,5,4,2};
Set<Integer> hs = new HashSet<Integer>();
for(int ar:arrays)
{
if(!hs.add(ar))
{
System.out.println("Dupicate is:" +ar);
}
}
/* Nested for loop */
int arraySearch[] = {2,2,3,4,5,6,7,7,7,8,10};
int m = 0; boolean flag = true;
for(int i=0; i<arraySearch.length; i++)
{
flag=true;
for(int j=i+1; j<arraySearch.length; j++)
{
if(arraySearch[i] == arraySearch[j])
{
m=arraySearch[i];
flag = false;
break;
}
}
if(flag == false)
{
System.out.println(m);
}
}
Both the approaches are giving correct result but the problem which I am facing is if there are two numbers repeated one after another its getting printed twice. say int[] arrays = {1,1,2,4,5,4,2};