# Easy-题目41：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 jis at most k.

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++) {
if(map.containsKey(nums[i]) && Math.abs(i-map.get(nums[i]))<=k)
return true;
else
map.put(nums[i],i);
}
return false;
}
}

16ms，beats 5.31%，众数14ms，30.23%
cmershen的碎碎念：

#### 【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II（包含重复元素II）】

2015-08-29 06:36:21

#### leetcode 219: Contains Duplicate II

2015-05-29 15:02:09

#### 219. Contains Duplicate II [easy] (Python)

2016-06-14 19:59:14

#### 219. Contains Duplicate II （python）

2016-10-30 16:11:05

#### LeetCode 219:Contains Duplicate II

2015-05-31 18:41:22

#### LeetCode 219. Contains Duplicate II（C++版）

2017-03-31 13:39:15

#### leetcode解题之219 # Contains Duplicate II Java版 (数组中出现重复元素）

2017-03-18 22:12:04

#### [leetcode-219]Contains Duplicate II（Ｃ）

2015-08-26 11:03:54

#### 219. Contains Duplicate II

2015-06-03 16:58:33

#### 219. Contains Duplicate II *

2016-08-29 19:36:56