The approach that comes to mind:
Have an (initially empty) heap of objects containing a word and its position - order this heap by the position.
smallestDistance = infinity
biggestSize = 0
for each word in the input:
heap.insert(Pair(current position, word))
hashMap[word] = current position
// We saw the word after this occurrence, so remove it
while hashMap[heap.minimum.word] != heap.minimum.position
heap.pop()
// We found another word!
// The previous smallest is invalid, since it doesn't contain this word
if heap.size > biggestSize
smallestDistance = currentPosition - heap.minimum
biggestSize = heap.size
// Is this distance better than the best so far? If so, use it instead
else
smallestDistance = min(smallestDistance, currentPosition - heap.minimum)
output smallestDistance