Contains Duplicate
Check whether any value appears more than once
DSA, Easy, Arrays & Hashing
Which structure or algorithm, and what does it cost.
A list nums holds whole numbers. Return true if some value shows up more than once in it. Return false if no value does. Numbers can be negative or very large.
Examples
Constraints
- 1 <= length of
nums<= 100,000. - Each number is a whole number between -1,000,000,000 and 1,000,000,000.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(n) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Think about what you must remember while you read the list from left to right, so that a repeat is easy to spot.
Hint 2 the approach
Keep every value you have already seen in a structure with fast lookup. Before you add a new value, ask whether it is already there.
Hint 3 the key step
Use a set. What must you ask the set before you add the current number?
Walkthrough
Full walkthrough brute force to best
Brute force
Compare every pair of positions and stop when two values are equal. A list of n numbers has about n squared over two pairs, so this is O(n^2) time and O(1) extra memory. For 100,000 numbers that is about 5 billion comparisons, too slow.
Better method
Sort a copy of the list. Equal values now sit next to each other, so one pass that compares each value with its neighbour finds any repeat. That is O(n log n) time for the sort and O(n) memory for the copy. It is good, and it is the right choice if you cannot afford a set.
Best method
Read the list once and keep a set of the values seen so far. For each number, a lookup in the set tells you in constant time on average whether it came before. If it did, you found a repeat and you stop. If not, add it and go on. The check must come before the add, or every number would match itself. If the loop ends with no match, every value is different. Each number is looked up once, so the total work is linear.
Hand trace
Run on [14, 3, 9, 27, 3]. Number 14: the set is empty, so add it. Set {14}. Number 3: not in the set, add. Set {14, 3}. Number 9: add. Number 27: add. Set {14, 3, 9, 27}. Number 3: it is in the set, so return true. The loop stopped at the last number, and the answer is true.
Code
def has_duplicate(nums):
seen = set()
for x in nums:
if x in seen:
return True
seen.add(x)
return FalseTime and memory
O(n) time, because each number needs one set lookup and one insert, both O(1) on average. O(n) memory, because the set can hold every number when all of them are different.
Common mistakes
- Adding the number to the set before the check. Then every number finds itself and the answer is always true.
- Sorting the list and then comparing numbers that are not neighbours. After the sort only adjacent values can be equal, so compare each value with the next one only.
- Using a list of flags indexed by value. The values go up to a billion and can be negative, so a set or a dictionary is the right tool.