---
# System prepended metadata

title: \#219 Contains Duplicate II
tags: [leetcode, leetcode-easy]

---

# \#219 Contains Duplicate II
## *檢查陣列內是否有重複元素, 且兩元素索引至少距離k*
## Log
 - build 20210430 by syhuang

## 初見
 - 用map紀錄每個元素最後出現的位址, 跑迴圈判斷重複出現時就檢查距離, 符合條件即回傳
```javascript=
var containsNearbyDuplicate = function(nums, k) {
    var map = {};
    var i = 1;
    for(var n of nums){
        if(map[n]){
            if(i - map[n] <= k) return true;
        }
        map[n] = i;
        i++;
    }
    return false;
};
```
## 備註
 - 初見即畢業
## 參考
###### tags: `leetcode`, `leetcode-easy`