It should return false if every element is distinct
#Approach one: Linear Search
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
for i in range (0,len(nums)):
for j in range (i+1,len(nums)) :
if (nums[j] == nums[i]) :
return True;
return False;
Complexity Analysis:
Time:O(n^2)
Space:O(1)
#Approach two:Sorting
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
nums.sort()
for i in range(0,len(nums)-1):
if nums [i] == nums[i+1]:
return True
return False
Complexity Analysis:
Time:O(nlogn)
Space:O(1)
#Approach three : a pythonic method
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(nums) != len(set(nums))