Last active
March 8, 2018 11:19
-
-
Save stepheweffie/281fbde2f2ecdfa08d94a3822d42cf92 to your computer and use it in GitHub Desktop.
first complete duplicate pair of a list, not the fastest
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 firstDupe(a): | |
""" | |
Finding the first occurrence of any duplicates with the first second occurrence of a value | |
as long as params are met. | |
Returns -1 if outside params. | |
""" | |
if len(a) == len(set(a)): | |
return -1 | |
indices = [a.index(i) for i in a] | |
ilist = list() | |
if 1 <= len(a) <= 10 ** 5: # param 1 | |
for i in range(0, len(a)): | |
if indices[i] <= len(a): # param 2 | |
if indices[i] != i: | |
ilist.append((i, a[i])) # ilist appends the second index occurrence | |
# of the dupe value | |
else: | |
return -1 | |
else: | |
return -1 | |
#print(ilist) | |
if len(ilist) == 0: | |
return -1 | |
n = [(z, x) for z, x in ilist] | |
print(n, min(n)[1]) | |
return min(n)[1] | |
firstDupe([6, 8, 4, 9, 1, 4, 12, 5, 3, 5, 10, 12]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment