# 217. Contains Duplicate

## Description

See <https://leetcode.com/problems/contains-duplicate/>

## Solutions

There are numerous solutions to this problem. Here are a few.

### Solution 1

```python
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        return len(set(nums)) != len(nums)
```

Space O(N), Time O(N) - from creating the set

### Solution 2

```python
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        cache = defaultdict(int)
        for i in nums:
            if i in cache:
                return True
            else:
                cache[i] = i
        return False
```

Space O(N), Time O(N)

### Solution 3

This would be the slowest solution as it has the overhead of sorting the list.

```python
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        sorted_list = sorted(nums)
        for i in range(len(sorted_list) - 1):
            if sorted_list[i] == sorted_list[i+1]:
                return True
        return False
```

Space O(N), Time O(N log N) - Time complexity for sort. See: <https://wiki.python.org/moin/TimeComplexity>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://monica-granbois.gitbook.io/cs-theory-and-problems/problems/leetcode/217.-contains-duplicate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
