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.
Hide Tags
Show Similar Problems
--------------------------
Using hash table: map OR unordered_map.
Since we don't need the orders in the map, we can use unordered_map to speed up the process.
//////////////////////////////////////////////
//code
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int,int> tmp;
int n=nums.size();
for(int i=0;i<n;i++){
tmp[nums[i]]=i;
if(tmp.size()<i+1)return true;
}
return false;
}
};
No comments:
Post a Comment