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.

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

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;
    }
};

liuqinh2s
42 声望13 粉丝

认真写代码,搞创作