In this problem, the objective is to return true if any value is presented in the array at least twice and to return false if each value is distinct.
An example test case:
Input: nums = [1,2,3,1]
We will create a set of the original array. Then, we’ll make a new array and populate it with the values of the set by using the spread operator.
Since a set will only include unique values (you can imagine it as if the duplicates were getting tossed out), we can just compare the length of the new array with the argument. If there were any duplicates, the length wouldn’t be the same.
Of course, there are many ways to solve a problem, so feel free to share your thoughts in the comments below!
I realize that this doesn’t actually solve the problem directly, so here is another way to solve the problem:
As mentioned in this article, we can use a dictionary to keep track of the numbers and the tally for each. Then, if we are able to find a number that has a tally of anything over 1, we’ll return true.
Ready for a Medium level LeetCode problem?
Using a Dictionary with a Medium Level LeetCode: Find the Duplicate Number
In this series, I will explain my thought process of figuring out one way to solve a particular LeetCode problem and…
☕️If you enjoyed this article, please consider tipping here: