# 存在重复元素 II

<p>给定一个整数数组和一个整数&nbsp;<em>k</em>，判断数组中是否存在两个不同的索引<em>&nbsp;i</em>&nbsp;和<em>&nbsp;j</em>，使得&nbsp;<strong>nums [i] = nums [j]</strong>，并且 <em>i</em> 和 <em>j</em>&nbsp;的差的 <strong>绝对值</strong> 至多为 <em>k</em>。</p>

<p>&nbsp;</p>

<p><strong>示例&nbsp;1:</strong></p>

<pre><strong>输入:</strong> nums = [1,2,3,1], k<em> </em>= 3
<strong>输出:</strong> true</pre>

<p><strong>示例 2:</strong></p>

<pre><strong>输入: </strong>nums = [1,0,1,1], k<em> </em>=<em> </em>1
<strong>输出:</strong> true</pre>

<p><strong>示例 3:</strong></p>

<pre><strong>输入: </strong>nums = [1,2,3,1,2,3], k<em> </em>=<em> </em>2
<strong>输出:</strong> false</pre>


## template

```java
public class Solution {
	public boolean containsNearbyDuplicate(int[] nums, int k) {
		int left = 0;
		int right = -1;
		HashMap<Integer, Integer> hashMap = new HashMap<>();
		while (left < nums.length) {
			if (right + 1 < nums.length && right - left < k) {
				right++;
				if (hashMap.containsKey(nums[right])) {
					return true;
				} else {
					hashMap.put(nums[right], 1);
				}
			} else {
				hashMap.put(nums[left], hashMap.get(nums[left]) - 1);
				if (hashMap.get(nums[left]) == 0) {
					hashMap.remove(nums[left]);
				}
				left++;
			}
		}
		return false;
	}
}
```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```