# Contains Duplicate

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: [1,2,3,1] Output: true
Example 2:
Input: [1,2,3,4] Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2] Output: true```
``````var containsDuplicate = function(nums) {
let seen={};
for (let i=0;i<nums.length;i++) {
if (seen[nums[i]]) {
return true;
}
seen[nums[i]]=1;
}
return false;
};``````

Approach:
We want to keep track of what we’ve seen before, in order to check if a particular element is a duplicate. Here we use an object to store what we’ve seen before. Other than object data structure, we could use the (relatively) new Javascript data structure Map.
The reason we use object/Map data structure is for fast O(1) lookups when we want to check if an element has been seen before.
The reason we don’t use an array with the elements as index, is because there could be large gaps in the array.

Code Breakdown:
Let’s go over the important sections of the code.

``````    for (let i=0;i<nums.length;i++) {
if (seen[nums[i]]) {
return true;
}
seen[nums[i]]=1;
}``````

We iterate through each element of the input array.
If it’s been seen before, we return true since there’s at least one duplicate.
We then set that element’s value as key to our seen object. Thus if it gets seen again, it’ll trigger the above.
If it gets to the end without triggering that, we return false since we didn’t find any duplicates.