O(n) c++ solution using unordered_set


  • 1
    M
    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            unordered_set<int> duplicates ;
            for(int i = 0 ; i < nums.size() ; i++){
                if(!duplicates.insert(nums[i]).second) {
                    return true ;
                }
            }
            return false;
        }
    };

Log in to reply
 

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