• + 0 comments

    Solution on Python3

    from collections import Counter
    n = int(input())
    elements = list(map(int, input().split(' ')))
    print("-1" if len(set(elements)) == n else "", end="")
    if len(set(elements)) != n:
        repeated_list = [_ for _ in Counter(elements) if Counter(elements).get(_) > 1]
        less_distance = []
        for i in repeated_list:
            indices = [index for index, j in enumerate(elements) if j == i]
            less_distance.append(abs(indices[0] - indices[1]))
        print(min(less_distance))