2 Lines 40ms C++ Solution


  • 4
    D
    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            return unique(nums.begin(), nums.end()) != nums.end();
        }
    };
    

    Complexity: O(n log n + n)


  • 3
    F

    Hi! This is my simple C++ solution using hash_set. Time complexity: O(n)

    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            unordered_set<int> hset;
            for(auto &v : nums) {
                if(hset.find(v) == hset.end())
                    hset.insert(v);
                else
                    return true;
            }
            return false;
        }
    };

  • 0
    H

    hset finding a element need time too.


Log in to reply
 

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