Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
如果数组中有重复的数字就返回true,否则返回false。
用hashset查重即可。
class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums.length == 0)return false;
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0; i< nums.length; i++){
if(set.contains(nums[i])){
return true;
}else{
set.add(nums[i]);
}
}
return false;
}
}
网友评论