um trying to check whether there are duplicate values exists in an integer list by python . this was successful and I found that the execution time getting higher when the size of the list getting increase. How may I improve the run time of the following logic?
def containsDuplicate( nums):
if len(nums) < 2:
return False
cnt = 0
flag = False
length = len(nums)
while cnt < length:
p = cnt + 1
while p < length:
if nums[cnt] == nums[p]:
flag = True
break
p += 1
cnt += 1
return flag
You could use a set:
EDIT: a faster version, as pointed out in the comments: