You are viewing a single comment's thread. Return to all 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;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Distances
You are viewing a single comment's thread. Return to all comments →
** Java 8 *
public static int minimumDistances(List a) { Map map = new HashMap<>(); int minDistance = 9999;
}