leetcode讲解--217. Contains Duplicate

217. Contains Duplicate

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.app

so easy的一道题。我直接上代码吧:code

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> hash;
        for(auto i:nums){
            hash[i]++;
        }
        for(auto i:hash){
            if(i.second>=2)
                return true;
        }
        return false;
    }
};
相关文章
相关标签/搜索