C++ solution with comments


  • 0
    R

    class Solution {
    public:
    bool containsDuplicate(vector<int>& nums) {

        unordered_set<int> h;
        h.clear();
        if (nums.size() <= 1 ) return false; // Base case return false if there is one/no elements;
        for(int i=0; i<nums.size(); i++){
            
            if(h.find(nums[i]) != h.end()){     // At each iteration check whether there is any duplicate element;
                return true;
            }
            h.insert(nums[i]);                     // insert elements;
        }
        return false;
    }
    

    };


Log in to reply
 

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