Why is hash slower than sort?


  • 0
    T

    The fist block of code runs in 40 ms but the second part runs in 50 ms. Why is that the case?

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

Log in to reply
 

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