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 then provide the JavaScript code.

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:

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:



Software Engineer📍Tokyo. ☕️

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store