← DSA notes
Contains Duplicate
Link
Pattern
Approach
Yeah this would be a very simple approach where we do the initialize as set as an empty variable is a seen then iterate over the nums. If we find the numbers in a seen set then we will return true. Otherwise we will add that to a seen set. After all iteration if you don't find any repetition, we simply return false.
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for i in nums:
if i in seen:
return True
seen.add(i)
return False