• + 0 comments

    ** Java 8 *

    public static int minimumDistances(List a) { Map map = new HashMap<>(); int minDistance = 9999;

    for (int i = 0; i < a.size(); i++) {
        int currentKey = a.get(i);
        if (map.containsKey(currentKey)) {
            minDistance = Math.min(minDistance, Math.abs(i - map.get(currentKey)));
        }
        map.put(currentKey, i);
    }
    return minDistance != 9999 ? minDistance : -1;
    

    }