> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/hash-table/various-implementation-using-stl/contains-duplicate.md).

# 3.Contains Duplicate

## Type I:

Given an array of integers, find if the array contains any duplicates.\
Your function should **return true if any value appears at least twice in the array**, and it should **return false if every element is distinct.**

```
Example 1:
Input: nums = [1,2,3,1]
Output: true

Example 2:
Input: nums = [1,2,3,4]
Output: false

Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
```

### Solution: (Using Hash set)

```
Example 1:
Input: [1,2,3,1]
Output: true

Example 2:
Input: [1,2,3,4]
Output: false
```

```cpp
class Solution
{
public:
    bool containsDuplicate(vector<int> &nums)
    {

        if (nums.size() == 0)
        {
            return false;
        }

        unordered_set<int> s;
        bool res = false;
        for (int i = 0; i < nums.size(); i++)
        {
            if (s.find(nums[i]) == s.end())
            {
                s.insert(nums[i]);
            }
            else
            {
                res = true;
                break;
            }
        }

        return res;
    }
};
```

## Type II:&#x20;

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that **nums\[i] = nums\[j]** and the **absolute** difference between i and j is at most k.

```
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true

Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true


Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
```

### Solution: (Using Hashmap)

```
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true

Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
```

**Solution :**

```cpp
class Solution
{
public:
    bool containsNearbyDuplicate(vector<int> &nums, int k)
    {

        unordered_map<int, vector<int>> m;
        bool res = false;
        for (int i = 0; i < nums.size(); i++)
        {

            if (m.find(nums[i]) == m.end())
            {
                vector<int> v;
                v.push_back(i);
                m.insert({nums[i], v});
            }
            else
            {
                auto itr = m.find(nums[i]);
                vector<int> v;
                v = itr->second;
                v.push_back(i);
                m[nums[i]] = v;
            }
        }

        for (auto itr = m.begin(); itr != m.end(); itr++)
        {
            vector<int> v = itr->second;

            if (v.size() > 0)
            {
                int i = 0;
                while (i < v.size() - 1)
                {
                    if (v[i + 1] - v[i] <= k)
                    {
                        res = true;
                        break;
                    }
                    i++;
                }
            }
        }

        return res;
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://soumyajit4419.gitbook.io/ds-algo/hash-table/various-implementation-using-stl/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.
