A 32ms Simple C++ Solution


  • 0
    Y
    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_map<int,int> map;
            
            for(int i = 0; i < nums.size(); i++) {
                if(map.find(nums[i]) != map.end()) if(i - map[nums[i]] <= k) return true;
                map[nums[i]] = i;
            }
            return false;
        }
    };

Log in to reply
 

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