Contains duplicate
July 10, 2022
array-and-hashmapProblem URL: Contains duplicate
We are given a list of integers. We have to find out whether we have any duplicate or not. We can store the elements in a hashset while iterating through the list. We the item is already present in the hashset then we immediately return true
as we already have duplicate. If we finish the iteration of the whole list, that means we don't have any duplicate, so we return false
.
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
hashset = set()
for i in range(len(nums)):
if nums[i] in hashset:
return True
hashset.add(nums[i])
return False
Time Complexity: O(n)
Space Complexity: O(n)