C++ Set solution, no loops (using constructor)

  • 0
    bool containsDuplicate(vector<int>& nums) {
            set<int> myset(nums.begin(), nums.end());
            if(myset.size() < nums.size()) return true;
            return false;

    You don't need to iterate over the set at all, if it's smaller you know that there are duplicates.

Log in to reply

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