Meta Data#
Difficulty: easy First Attempt: 2022-08-21
- Submissions: Wrong Answer → Accepted → Wrong Answer → Accepted ×4
Approach#
set(nums) drops duplicates, so a shorter length means a repeat (Hash Table). To stop early, add elements to a set while scanning and return True on the first one already present.
My Attempts#
In both 2022-08 and 2022-10 I wrote len(set(nums)) == len(nums), the exact opposite, returning True when there is no duplicate. != passed.
Solution#
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for x in nums:
if x in seen:
return True
seen.add(x)
return False
Complexity#
Time O(n), space O(n).
Mistakes to Avoid#
- A repeat exists when the lengths differ; do not invert it.
Interview Explanation#
I add numbers to a set while scanning and report a duplicate as soon as one is already present, in O(n) time and space; comparing the set’s length with the array’s also works.
