# 存在重复元素 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

```python
class Solution:
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """

        if len(list(set(nums))) == len(nums):
            return False
        left = 0
        right = left + k
        if k >= len(nums):
            return len(list(set(nums))) < len(nums)
        while right < len(nums):
            while left < right:
                if nums[left] == nums[right]:
                    return True
                else:
                    right -= 1
            left += 1
            right = left + k
        if len(list(set(nums[left:]))) < len(nums[left:]):
            return True
        return False

```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```