描述
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.
樣例
Given nums = [1,1]
, return ture
.
無難度題目,用關聯容器即可
class Solution {
public:
/**
* @param nums: the given array
* @return: if any value appears at least twice in the array
*/
bool containsDuplicate(vector<int> &nums) {
// Write your code here
unordered_map<int,int> mmap;
for(int i=0;i<nums.size();i++){
mmap[nums[i]]++;
if(mmap[nums[i]]>=2) return true;
}
return false;
}
};