I am a TOTAL Noob so please be gentle!
I have written this code:
def sorted_has_duplicate(numbers):
numbers.sort()
duplicate = False
for num in range(len(numbers)-1):
if numbers[num] == numbers[num + 1]:
duplicate = True
break
return duplicate
The code runs fine and it does what it is supposed to do which is find the first duplicate value in a list if any and return True or False
.
My question is how can I have the same function perform the same task if the list is not sorted (sorting is not allowed) and i can only use built in python functions?