Consider the previous and next index of the each element.
IntuitionWe can track the index of each element in a hashmap. Then the idea is to just use the stored indices of each element, compare the previous and next ind
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We can track the index of each element in a hashmap. Then the idea is to just use the stored indices of each element, compare the previous and next index and only consider the minimum. We can make use of binary search in the List of indices that we store for efficiency.

# Approach
<!-- Describe your approach to solving the problem. -->
* Traverse the array and store index in a hashmap.
* Traverse through the query and for each query, see how many elements in the hashmap is stored by checking size.
* If size is 1, then no more repeating elements, return -1.
* Else, we need to consider previous and next elements keeping cases to consider if the index we are working on is at the start of the index list or end of index list in the hashmap.
* Binary search to find where our query index is inside the hashmap list of index.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(length of array) + O(length of query * log(size of hashmap indices list))

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(N) for storing the indices list.

# Code
```java []
class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int numsLength = nums.length;
        int queriesLength = queries.length;
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < numsLength; i++) {
            int num = nums[i];
            if (!map.containsKey(num)) {
                map.put(num, new ArrayList<>());
                map.get(num).add(i);
            }
            else {
                map.get(num).add(i);
            }
        }
        int index = 0;
        List<Integer> result = new ArrayList<>();
        for (int query : queries) {
            List<Integer> indices = map.get(nums[query]);
            //System.out.println(indices);
            if (indices.size() == 1) {
                result.add(-1);
            }
            else {
                // int previous = Integer.MAX_VALUE;
                // int next = Integer.MAX_VALUE;
                int currentIndex = binarySearch(indices, 0, indices.size() - 1, query);
                //System.out.println(currentIndex);
                if (currentIndex == indices.size() - 1) {
                    int next = numsLength - indices.get(currentIndex) + indices.get(0);
                    int previous = indices.get(currentIndex) - indices.get(currentIndex - 1);
                    result.add(Math.min(next, previous));
                }
                else if (currentIndex == 0) {
                    int next = indices.get(currentIndex + 1) - indices.get(currentIndex);
                    int previous = numsLength - indices.get(indices.size() - 1) + indices.get(currentIndex);
                    result.add(Math.min(next, previous));
                }
                else {
                    int previous = indices.get(currentIndex) - indices.get(currentIndex - 1);
                    int next = indices.get(currentIndex + 1) - indices.get(currentIndex);
                    result.add(Math.min(next, previous));
                }
            }
        }
        return result;
    }
    
    private int binarySearch(List<Integer> indices, int low, int high, int key) {
        while (low <= high) {
            int middle = low + (high - low) / 2;
            if (indices.get(middle) == key) {
                return middle;
            }
            if (indices.get(middle) > key) {
                high = middle - 1;
            }
            else {
                low = middle + 1;
            }
        }
        return -1;
    }
}
```
['Hash Table', 'Binary Search', 'Java']