Repeat Code With LeetCode — Contains Duplicate

Guess what? It’s another…spoiler…hashmap problem

Evan SooHoo
4 min readApr 18, 2024
Photo by Pickawood on Unsplash. BaseCS likens a hash table to a library.

This is the first warm-up problem in Grokking The Coding Interview, and it is a LeetCode easy. There are some easy LeetCode questions that I think make good practice because of the techniques they require, but this is not one of them — it’s more or less a repeat of the anagrams question, which I have already covered. It is still useful if you have never solved a hashmap problem before.

Grokking The Coding Interview deserves some discussion, but things are getting kind of murky. Once upon a time, DesignGurus and Educative content all fell under the Educative.io umbrella, so for a flat fee you had access to Grokking The Coding Interview, Grokking The System Design Interview, and a wide variety of other Educative.io content that consisted of everything from Docker to Elixir. Now DesignGurus and Educative are separate entities (the creator confirmed it on Reddit here) and for some reason Educative is rolling out their own similarly-worded content, like Grokking The MODERN System Design Interview.

None of the above was an affiliate link, and I have no intention of asking for one anytime soon. It is all just too confusing, and I do not want to gripe about what Educative.io did because I am missing critical context. For all I know, the two parted on amiable terms.

What this means for you, the reader, is that you have the option to buy lifetime subscriptions to DesignGurus courses like Grokking The Coding Interview, monthly subscriptions to DesignGurus, or monthly subscriptions to Educative.io to try out their more varied content. I personally think the lifetime subscription is the best option, but I have no idea what the legal ramifications would be if DesignGurus were ever to retire (would we lose website access)? Plus, these are a lot of paid resources. Not every software engineer intern, computer science undergrad, and self-taught developer has access to all of these things, especially if on top of that they are considering LeetCode Premium.

Some Free Resources:

The fundamental idea here is that regardless of what list you choose, you are probably better off learning patterns than trying to do every LeetCode problem.

Question

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Problem Link

Difficulty: Easy

Skills Required: Sorting (depending on implementation), hashmaps (depending on implementation)

Trivia: This is the first warm-up question in Grokking The Coding Interview

Solution

Here is how I imagine the thought process is supposed to go

Interviewer: Given an integer array nums, return true if any value appears at least twice in the array , and return false if every element is distinct

Candidate: Well, kind interviewer, I suppose I could use brute force. Compare every element to every other element. If we obtain a match at any point, then there is indeed a duplicate

Interviewer: That would be fine, but I am interviewing 18 other applicants. Can you do better?

Candidate: Well I suppose sorting the list would be O(n log n), so perhaps we can use a hashmap?

Interviewer: That will pass, but just so you know eight other candidates already did the same thing. If you do this correctly you will still have a system design interview, a behavioral interview, a take-home challenge, an HR challenge, and a 500-word essay

Candidate: What?

Interviewer: What?

This is C++. You also can solve the problem with std::sort, but a hashmap is better with a time complexity of O(n) (someone else’s explanation and code is here).

Below is the best explanation of a hash table, which is very close to a hashmap with some caveats

I use an unordered_map, which is another close approximation. Here is a definition:

A hash table is a data structure that is used to store keys/value pairs. It uses a hash function to compute an index into an array in which an element will be inserted or searched. By using a good hash function, hashing can work well. Under reasonable assumptions, the average time required to search for an element in a hash table is O(1)
— -Source

I admit that the line

u1[nums[i]]++

is confusing, I have just written it so many times that I have gotten used to it. The for loop is just a way to get counts. We are populating the unordered_map with key/value pairs, where each key is a number and each value is the number of times that number has appeared. At the end we see if anyone has a count greater than or equal to 2.

Closing Thoughts

Note that the problem solved here is “contains duplicate,” not the medium variant Joma is solving

--

--

Evan SooHoo

A software engineer who writes about software engineering. Shocking, I know.