问题描述:
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
判断数组中的元素是否独特
用hashset实现,没什么说的:
class Solution {
public boolean containsDuplicate(int[] nums) {
Set <Integer> hset = new HashSet<>();
for (int i=0; i<nums.length; i++){
if (hset.contains(nums[i])){
return true;
}
else{
hset.add(nums[i]);
}
}
return false;
}
}
时间复杂度:O(n)