[LeetCode][JavaScript]Contains Duplicate II

Contains Duplicate II 

Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

https://leetcode.com/problems/contains-duplicate-ii/


数组中两个相同值的数,下标之差要小于等于k。

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} k
 4  * @return {boolean}
 5  */
 6 var containsNearbyDuplicate = function(nums, k) {
 7     var map = {};
 8     for(var i in nums){
 9         if(map[nums[i]]){
10             if(Math.abs(map[nums[i]] - i) <= k){
11                 return true;
12             }
13         }
14         map[nums[i]] = i;
15     }
16     return false;
17 };
原文地址:https://www.cnblogs.com/Liok3187/p/4540426.html