Last active
June 22, 2022 12:18
-
-
Save morisasy/458be325d8e310f6abc7c5caf37c9681 to your computer and use it in GitHub Desktop.
Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does.…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def firstDuplicate(a): | |
""" This function return a first doublicate element if it exit and return -1 if it does not .""" | |
new_set = set() | |
for i in a: | |
if i in new_set: | |
return i | |
else: | |
new_set.add(i) | |
if len(new_set)== len(a): | |
return -1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment