Contains Duplicate II | LeetCode 219

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard


Here is the full implementation.

let containsNearbyDuplicate = function(nums, k) {
  let numsMap = new Map();

  for (let i = 0; i < nums.length; i++) {
    let num = nums[i];

    if (numsMap.has(num) && i - numsMap.get(num) <= k) {
      return true;
    } else {
      numsMap.set(num, i);
    }
  }

  return false;  
};