Find the nearest repeated entries in an array
Nearest_repetition.cpp NearestRepetition.javapublic static int findNearestRepetition(List<String> s) {
Map<String, Integer> stringToLocation = new HashMap<>();
int closestDis = Integer.MAX_VALUE;
for (int i = 0; i < s.size(); ++i) {
if (stringToLocation.containsKey(s.get(i))) {
closestDis = Math.min(closestDis, i - stringToLocation.get(s.get(i)));
}
stringToLocation.put(s.get(i), i);
}
return closestDis;
}