Easy to understand C++ solution | Hashmap
Code
# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        unordered_map<int, int> firstOcc, lastOcc;
        vector<int> closest(n, -1);
        for (int j = 0; j < n; ++j) {
            if (firstOcc.count(nums[j]) == 0) {
                firstOcc[nums[j]] = j;
            } else {
                int i = lastOcc[nums[j]];
                setClosest(i, j - i, closest);
                setClosest(j, j - i, closest);
            }
            lastOcc[nums[j]] = j;
        }

        for (auto [num, j] : lastOcc) {
            int i = firstOcc[num];
            if (i != j) {
                setClosest(i, n + i - j, closest);
                setClosest(j, n + i - j, closest);
            }
        }
        
        vector<int> result;
        for (int q : queries) {
            result.push_back(closest[q]);
        }
        return result;
    }

private:
    void setClosest(int pos, int val, vector<int> &closest) {
        int n = closest.size();
        if (closest[pos] == -1) {
            closest[pos] = val;
        } else {
            closest[pos] = min(closest[pos], val);
        }
    }
};
```
['Hash Table', 'C++']