# Contains Duplicate LeetCode C++

• DSA

LeetCode Contains Duplicate problem, to check if the given array contains duplicates or not, if yes return True else return False

Difficulty: Easy

## Description

This is an easy problem and can be solved in one iteration as we start from the beginning if we find any number that we have previously encountered we can just return TRUE else we reach the end of the array and return FALSE

## Solution Explanation

As seen above we use only one for loop and we use it to iterate through the array once, as we iterate we use the count of [i] in [seen] i.e we created a new vector/array [seen] to store the elements that we have already iterated and if ‘i’ is present the count returns 1 else 0

For the first time, any element will not be present, so in the else statement it will be inserted into [seen] array later when the same comes next time the [count] return 1 and we return TRUE. In case there are no duplicates we exit the loop and return FALSE

Link to Problem: Contains Duplicate LeetCode

## Code for Contains Duplicate in C++

``````class Solution {
public:
bool containsDuplicate(vector < int > & nums) {
set < int > seen;
for (int i: nums)
if (seen.count(i))
return true;
else
seen.insert(i);
return false;
}
};``````

## Conclusion

This is an easy problem and I guess there are many ways to solve this, I used this approach and I have clearly understood the process, hope you understood it well, more Competitive Problems and Solutions by me at LeetCode Series