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.

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        if(nums.size()==0)
            return false;
        unordered_map<int,int> hashMap;
        for(auto num:nums)
            ++hashMap[num];
            
        for(auto tmp:hashMap)
        {
            if(tmp.second>1)
                return true;
        }
        
        return false;
    }
};

 

posted on 2017-03-05 05:22  123_123  阅读(99)  评论(0编辑  收藏  举报