Using a Hashmap - O(n) | C++, Python, Java
ApproachWe use a hashmap to store a list of indices of each number in nums. Then for each group (unique nums[i]) we compute the circular distances for each inde
# Approach
<!-- Describe your approach to solving the problem. -->
We use a hashmap to store a list of indices of each number in `nums`. Then for each group (unique `nums[i]`) we compute the circular distances for each index. The closest index will either be the next one in front or behind it (also considering the array is circular). Appending the indices to the lists in order also makes sure that the elements in front or behind are our candidates for the closest indices.

If a number appears only once we can set its corresponding position in `nums` to `-1`. The forward distance is the minimum of either the direct distance `abs(arr[i] - f)` or wrapping around the end of the array `nums.size() - arr[i] + f`, likewise for backwards (just a little different for wrapping around). The final value stored in `nums[arr[i]]` is the minimum of the forward and backward distances.

After that we can process each query since we can directly store the values back in `nums`.
# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = queries.size(), sz = nums.size();
        unordered_map<int, vector<int>> indices;
        for (int i = 0; i < nums.size(); i++){
            indices[nums[i]].push_back(i);
        }
        for (auto& pair : indices){
            auto arr = pair.second;
            int m = arr.size();
            if (m == 1){
                nums[arr[0]] = -1;
                continue;
            }
            for (int i = 0; i < m; i++){
                int f = arr[(i + 1) % m], b = arr[(i - 1 + m) % m];
                int forward = min(sz - arr[i] + f, abs(arr[i] - f));
                int backward = min(abs(b - arr[i]), arr[i] + sz - b);
                nums[arr[i]] = min(backward, forward);
            }
        }
        for (int i = 0; i < n; i++)
            queries[i] = nums[queries[i]];
        return queries;
    }
};
```
```python []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        sz = len(nums)
        indices = defaultdict(list)
        for i, num in enumerate(nums):
            indices[num].append(i)
        for arr in indices.values():
            m = len(arr)
            if m == 1:
                nums[arr[0]] = -1
                continue
            for i in range(m):
                f, b = arr[(i + 1) % m], arr[(i - 1 + m) % m]
                forward = min(sz - arr[i] + f, abs(arr[i] - f))
                backward = min(abs(b - arr[i]), arr[i] + (sz - b))
                nums[arr[i]] = min(backward, forward)
        for i in range(len(queries)):
            queries[i] = nums[queries[i]]
        return queries
```
```java []
class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int sz = nums.length;
        Map<Integer, List<Integer>> indices = new HashMap<>();
        for (int i = 0; i < sz; i++) {
            indices.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
        }
        for (List<Integer> arr : indices.values()) {
            int m = arr.size();
            if (m == 1) {
                nums[arr.get(0)] = -1;
                continue;
            }
            for (int i = 0; i < m; i++) {
                int j = arr.get(i);
                int f = arr.get((i + 1) % m), b = arr.get((i - 1 + m) % m);
                int forward = Math.min(sz - j + f, Math.abs(j - f));
                int backward = Math.min(Math.abs(b - j), j + (sz - b));
                nums[j] = Math.min(backward, forward);
            }
        }
        List<Integer> res = new ArrayList<>();
        for (int q : queries) {
            res.add(nums[q]);
        }
        return res;
    }
}
```
['Hash Table', 'C', 'Python', 'C++', 'Java', 'Python3']