217. Contains Duplicate
Code
class Solution
def containsDuplicate(self, nums: List[int]) -> bool:
hash_set = set()
for item in nums:
if item in hash_set:
return True
hash_set.add(item)
return False:
Time Complexity: O(n)
Space Complexity: O(n)
Iterating through each element in the input array "nums" and testing in O(1) time if we've encountered each value "x" previously. When we discover a duplicate, we return "True". We return "False" if the loop concludes without discovering any duplicates.
A hash table is used to implement the set data type in Python. On average, the hash table provides for efficient lookups and insertion of entries in constant time, implying that the time complexity of most set operations is O(1). When there are hash collisions, however, the time complexity can be O(n), where n is the number of members in the set.