This is a question on codefights:
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.
I've been struggling to figure out how to complete this in python. I'm unsure if I'm on the right path and if I am I can't seem to figure out how to access my index dictionary after finding the specific value from my d dictionary. I want to grab all the values that are greater than one in my d dictionary and then grab those values from index and then whichever value in index is smaller would be the answer.
If I'm going about this completely wrong then please let me know.
def firstDuplicate(a):
d = {}
index = {}
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
for i,e in enumerate(a):
if e in d:
index[e] = i
else:
index[e] = i
for key,val in d.items():
if val > 1: