You are viewing a single comment's thread. Return to all comments →
public static int minimumDistances(List<Integer> arr) { Map<Integer, LinkedList<Integer>> itemDistanceMap = new HashMap<>(); for(int i=0;i<arr.size(); i++) { int key = arr.get(i); if(itemDistanceMap.get(key) == null) { LinkedList<Integer> indices = new LinkedList<>(); indices.add(i); itemDistanceMap.put(key, indices); } else { itemDistanceMap.get(key).add(i); } } int minDistance = Integer.MAX_VALUE; Collection<LinkedList<Integer>> distances = itemDistanceMap.values(); for(LinkedList<Integer> distanceLst : distances) { if(distanceLst.size() > 1) { int distance = distanceLst.get(1) - distanceLst.get(0); minDistance = distance < minDistance ? distance : minDistance; } } return minDistance == Integer.MAX_VALUE ? -1 : minDistance; }
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 →