0
点赞
收藏
分享

微信扫一扫

通过hashset找到数组中重复的元素

whiteMu 2022-12-05 阅读 44


如何在hashset中快速找到重复的元素呢?方法很多,下面是其中一个办法:

int[] array = {1,1,2,3,4,5,6,7,8,8};

Set<Integer> set = new HashSet<Integer>();

for(int i = 0; i < array.length ; i++)
{

if(set.add(array[i]) == false)
{
System.out.println("Duplicate element found : " + array[i]);
}
}



利用了hashset晒选重复的特性,如果针对set中再增加重复的元素,就会报false了


举报

相关推荐

0 条评论