Using a Dictionary with a Medium Level LeetCode: Find the Duplicate Number
287. Find the Duplicate Number
The objective is to return whatever number is the repeated number in an array. We can assume that there will always be one.
An example test case:
Input: nums = [1,3,4,2,2]
Here is my initial thought process for breaking the problem down.
By using a dictionary, or a hash, we can keep track of the number of times a particular number is found.
In the last line, we are grabbing the key from the dictionary and returning it. However, we need to narrow it down to whichever key has a value that is greater than 1.
Of course, there are many ways to solve a problem, so feel free to share your thoughts in the comments below!
☕️If you enjoyed this article, please consider tipping here: