C++ using unordered_set and C# using HashSet


  • 0
    O

    C++ version

    bool containsDuplicate(vector<int>& nums) {
        if(nums.size() > 1){
           unordered_set<int> unset;
           pair<unordered_set<int>::iterator,bool> res;
            for(int i = 0; i <nums.size(); ++i)
            {
                res = unset.insert(nums[i]);
                if(res.second==false)
                    return true;
            }  
       }
        return false;
    }
    

    C# version

    public bool ContainsDuplicate(int[] nums) {
       if(nums != null && nums.Length > 1){
           HashSet<int> hashtable = new HashSet<int>();
            foreach(int i in nums)
            {
                if(!hashtable.Add(i))
                    return true;
            }  
       }
        return false;
    }

Log in to reply
 

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