My 48ms C++ solution using hash table


  • -1
    T
    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            unordered_map<int, int> mymap;
            int n=nums.size();
            int i;
            if(n<2)
                return false;
            
            mymap.insert({nums[0], 0});
            unordered_map<int, int>::iterator got;
            for(i=1; i<n; i++)
            {
                got = mymap.find(nums[i]);
                if(got != mymap.end())
                    return true;
                mymap.insert({nums[i], i});
            }
            return false;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.