Contains Duplicate, JavaScript, LeetCode
This is article will go through the Contains Duplicate question from the Array seciton of LeetCode’s Top Interview Questions (Easy Collection).
Here is the question as on LeetCode:
Get K. Johnson’s stories in your inbox
Join Medium for free to get updates from this writer.
The question is quite simple and the solution I wrote was straight forward. We want to return true if there are duplicate values in the array, and false if all values are unique.
I created a new Set at the beginning of the function to create seen, a set to hold unique values already gone over in the loop. The loop then checks to see if the current value is in the set. If it is not in the set, it is a unique value and we can add it to the set and continue. If the value is already in the set, then it is not a unique value and we return true and stop the loop since we can already determine that there is a duplicate in the array. If the loop finishes without finding a duplicate, then the function will return false.









