Untitled
Anonymous
plain_text
08/14/2026 12:51 PM
548 B
0
Indexable
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int, int> ans;
// key-> number , value -> last index it occured at
for(int i= 0; i < nums.size(); i++){
if( (ans.find(nums[i])!=ans.end()) && ( i-ans[nums[i]]<=k ) ){
return true;
}
ans[nums[i]]=i;
}
return false;
}
};Editor is loading...
Leave a Comment