Query Regarding Time complexity


  • 0
    P

    this is 33ms solution

     bool containsDuplicate(vector<int>& nums) {
           if(nums.size() <= 1) return 0;
        
        sort(nums.begin(), nums.end());
        
        for(int i = 0; i < nums.size() - 1; ++i){
            
            if(nums[i] == nums[i+1]) return 1;
            
        }
        
        return 0;
        }
    

    this is 46ms solution

     bool containsDuplicate(vector<int>& nums) {
            unordered_map<int,int> hash;
            for(int i=0;i<nums.size();i++)
            {
                if(hash[nums[i]]!=0)
                return true;
                hash[nums[i]]=i+1;
            }
            return false;
        }
    

    can anybody explain how?


Log in to reply
 

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