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.
直接使用hashtable即可
bool containsDuplicate(vector<int> &nums) {
unordered_set<int> set;
for (int i : nums) {
if (set.find(i) != set.end())
return true;
else
set.insert(i);
}
return false;
}
- Contains Duplicate: 判断一个数组是否存在重复元素
- Contains Duplicate II:判断一个数组在指定距离内是否存在重复元素
- Contains Duplicate III:判断一个数组在指定距离内是否存在元素距离小于t