Contains Duplicate leetcode

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.

Subscribe to see which companies asked this question

bool containsDuplicate(vector<int>& nums) {
    unordered_map<int, int> hash;
    for (auto i : nums)
    {
        if (hash.find(i) == hash.end())
            hash.insert(make_pair(i, 1));
        else
            return true;
    }
    return false;
}
原文地址:https://www.cnblogs.com/sdlwlxf/p/5116669.html