forked from mdmzfzl/NeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0217-contains-duplicate.py
23 lines (17 loc) · 1002 Bytes
/
0217-contains-duplicate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Problem: LeetCode 217 - Contains Duplicate
Key Idea:
To check if there are any duplicates in the given list, we can use a hash set (set in Python) to store the unique elements as we traverse the list. For each element, we check if it is already present in the set. If it is, then we have found a duplicate, and we return True. If we traverse the entire list without finding any duplicates, we return False.
Time Complexity:
The time complexity of this approach is O(n), where n is the size of the input list. In the worst case, we might need to traverse the entire list once to find a duplicate.
Space Complexity:
The space complexity is O(n), as the hash set can potentially store all elements of the input list if they are all distinct.
"""
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
hashset = set()
for n in nums:
if n in hashset:
return True
hashset.add(n)
return False