Python program to find the first duplicate element in a given array of integers. Return -1 If there are no such elements

def find_first_duplicate(nums):
num_set = set()
no_duplicate = -1

for i in range(len(nums)):

    if nums[i] in num_set:
        return nums[i]
    else:
        num_set.add(nums[i])

return no_duplicate

print(find_first_duplicate([1, 2, 3, 4, 4, 5]))
print(find_first_duplicate([1, 2, 3, 4]))
print(find_first_duplicate([1, 1, 2, 3, 3, 2, 2]))

1 Like