{"size": 837, "ext": "js", "max_stars_repo_path": "temp/219.js", "max_stars_repo_name": "flowmemo/leetcode-in-js", "max_stars_repo_stars_event_min_datetime": "2017-03-04T04:12:39.000Z", "max_stars_repo_stars_event_max_datetime": "2017-03-04T04:12:39.000Z", "max_issues_repo_path": "temp/219.js", "max_issues_repo_name": "flowmemo/leetcode-in-js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "temp/219.js", "max_forks_repo_name": "flowmemo/leetcode-in-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.0227272727, "max_line_length": 190, "alphanum_fraction": 0.5639187575}
/*
219. Contains Duplicate II
Given an array of integers and an integer k, find out whether 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.
*/

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var containsNearbyDuplicate = function (nums, k) {
  const map = new Map()
  const len = nums.length
  let pre
  for (let i = 0; i < len; i++) {
    pre = map.get(nums[i])
    if (pre !== undefined && i - pre <= k) return true
    map.set(nums[i], i)
  }
  return false
}

let tc = [
  [
    [1, 2], 1
  ],
  [
    [1, 3, 4, 3], 2
  ],
  [
    [1, 3, 4, 5, 3], 2
  ]
]

let ans = [
  false, true, false
]

for (let i = 0; i < tc.length; i++) {
  let a = ans[i]
  let r = containsNearbyDuplicate(...tc[i])
  console.log(a === r, a, r)
}
