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.
如果數(shù)組中有重復(fù)的數(shù)字就返回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;
}
}