7 lines concise and easy understand c++ solution


  • 4
    A
    class Solution {
    public:
        bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_map<int, int> mp;
            int i = 0;
            for(auto num : nums){
                if(mp.count(num) && mp[num] >= i - k) return true;
                mp[num] = i++;
            }
            return false;
        }
    };

Log in to reply
 

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