> 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/217.-contains-duplicate.md).

# 217. Contains Duplicate

\# Easy

{% hint style="info" %}
Three methods:

1\) use set, compare size of the set before and after insert a number; **set.insert()**

2\) use unordered\_map, search map if contain each number;&#x20;

**m.find(), m.end()**&#x20;

3\) first sort, then compare neighbor two numbers if equal.

**sort(num.begin(), nums.end()) change the original list**
{% endhint %}

{% tabs %}
{% tab title="C++方法一" %}

```cpp
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        // edge case
        if(nums.size() <= 1) return false;
        
        // regular case
        set<int> s;
        for(auto a: nums) {
            int n = s.size();
            s.insert(a);
            if(n == s.size()) return true;
        }
        return false;
    }
};
```

{% endtab %}

{% tab title="C++方法二" %}

```cpp
class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        // edge case
        if(nums.size() <= 1) return false;
        
        // regular case
        unordered_map<int, int> m;
        for(auto a : nums) {
            if(m.find(a) != m.end()) return true;
            m[a] = 1;
        }
        return false;
    }
};
```

{% endtab %}
{% endtabs %}
