数组中的重复数字
作者:互联网
代码实现部分(和原题不一样,返回所有重复数字而非第一个)
public class solution {
public static boolean duplicate(int numbers[],int length,int [] duplication) {
Set<Integer> set=new HashSet<>();
int j=0;
boolean flag=false;
for(int i=0;i<length;i++)
{
if(set.contains(numbers[i]))
{
duplication[j]=numbers[i];
j++;
flag=true;
}
else
{
set.add(numbers[i]);
}
}
return flag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int numbers[]={6,3,2,0,2,5,0};
int len=numbers.length;
int duplication[] = new int[len];
boolean flag=duplicate(numbers,len,duplication);
if(flag)
{
System.out.println(Arrays.toString(duplication));
}
}
}
关于java中的hashset
标签:set,数字,重复,int,flag,boolean,numbers,数组,duplication 来源: https://www.cnblogs.com/Sharon0715/p/14198395.html