diff --git a/CPP/Problems/Leetcode-219.cpp b/CPP/Problems/Leetcode-219.cpp new file mode 100644 index 00000000..fc4bda94 --- /dev/null +++ b/CPP/Problems/Leetcode-219.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + bool containsNearbyDuplicate(vector& nums, int k) { + unordered_set seen; + + for (int i = 0; i < nums.size(); ++i) { + if (!seen.insert(nums[i]).second) + return true; + if (i >= k) + seen.erase(nums[i - k]); + } + + return false; + } +};