---
description: '# Easy'
---

# 217. Contains Duplicate

{% 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 %}

