> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/bu-chong-6180-dao/219.-contains-duplicate-ii.md).

# 219. Contains Duplicate II

\# Easy

{% hint style="info" %}
Key idea: only two elements in the list are the same. Then this problem is easy. **unordered\_map**
{% endhint %}

{% tabs %}
{% tab title="C++" %}

```cpp
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> m;
        for(int i = 0; i < nums.size(); i ++) {
            if(m.find(nums[i]) != m.end() && i-m[nums[i]] <= k)
                return true;
            m[nums[i]] = i;
        }
        return false;
    }
};
```

{% endtab %}
{% endtabs %}
