• + 1 comment

    JavaScript solution:

    function minimumDistances(a) {
        let minimumDistance = a.length;
        for (let i = 0; i < a.length; i++) {
            const currentNumber = a[i];
            const nextNumberIndex = a.indexOf(currentNumber, i + 1);
            if (nextNumberIndex !== -1) {
                const indicesBetween = nextNumberIndex - i;
                if (minimumDistance > indicesBetween) {
                    minimumDistance = indicesBetween;
                }
            }
        }
        if (minimumDistance === a.length) {
            return -1;
        }
        return minimumDistance;
    }