Linear time & space w/ Dictionaries
IntuitionBasically, if I pre-process the nums array with two dictionaries (myDict and myPos) where the keys are the elements in nums and the value for each key
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Basically, if I pre-process the nums array with two dictionaries (myDict and myPos) where the keys are the elements in nums and the value for each key is a dictionary holding the location of all the element's indices.  I'll have all the indices of whatever element the current query is asking for. Then for each query use the dictionaries to grab the next index to the left and the next index to the right.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Iterate through the nums array and build the two dictionaries
- In myDict the key is the element in nums and it's value is another dictionary who's key:Value holds the unique indices of that element in nums, and the overall position in the dict as the value.
- In myPos the key is the element in nums and it's value is another dictionary who's key:Value pair holds the overall position in the dictionary, and the index of that element in nums.
2. Iterate through each query, init the minDistance (which will be updated during comparison of the left and right side), enter the if statement with the valueToMatch as the key to use for the dictionaries. 
3. Grab the dictionaries held as the value under the key valueToMatch from myDict and posDict and put them in idxDict and posDict respectively.
4. If the count of the dictionary isn't greater then 1, break the if statement and don't update the ans array with the minDistance. Otherwise grab the query index as queryIndex.
5. Now find the leftIndex
- If the (queryIndex - 1) is greater than or equal to 0 it's not on the left edge, and we can just calculate the distance for the index held in the posDict at (queryIndex - 1) and update minDsitance
- Otherwise we're on the left edge and have to circle back to the next index at the end of nums which is held at the value held at key of the dictionary's count.
6. Now find the rightIndex
-   If the (queryIndex + 1) is less than nums.count it's not on the right edge, and we can just calculate the distance for the index held in the posDict at (queryIndex + 1) and update minDsitance
-  Otherwise we're on the right edge and have to circle back to the next index at the start of nums which is at the value held at key 0
7. Update minDistance in the ans array at the current index in queries if it's been changed, otherwise keep the -1 it was initialized with.

<br><br><br>


Here are the contents of myDict and posDict for testCase for more clarity. 
- nums = [1,3,1,4,1,3,2]
- queries = [0,3,5]
- myDict = [1: [2: 1, 4: 2, 0: 0], 2: [6: 0], 4: [3: 0], 3: [5: 1, 1: 0]]
- myPos = [1: [2: 4, 0: 0, 1: 2], 2: [0: 6], 4: [0: 3], 3: [1: 5, 0: 1]]
1. Given a query (let's say 0), I know the key to use for the dictionaries is nums[query] (so 1).
2. myDict[nums[query]] so ([2: 1, 4: 2, 0: 0]) gives me all the unique indices of nums[query] in nums and the value provides its index in myDict. 
3. posDict[nums[query]] so ([2: 4, 0: 0, 1: 2]) gives me all the indices of the unqiue indices of nums[query] in nums.

The dictionaries are just reversed versions of themselves but given a query, you can get the index of itself and then use that to find the left and right index of nums[query].

<br><br><br>

# Complexity
- Time complexity: O(n + q) -> [iterate through n + iterate through q]
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n + q) -> [n nums in both dict & ans array size q]
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```swift []
class Solution {
    func solveQueries(_ nums: [Int], _ queries: [Int]) -> [Int] { 
        guard Set(nums).count > 1 else { return nums.count > 1 ? Array(repeating: 1, count: queries.count) : Array(repeating: -1, count: queries.count)}
        var myDict: [Int : [Int : Int]] = [:], ans: [Int] = Array(repeating: -1, count: queries.count), myPos: [Int: [Int : Int]] = [:] 

        for (index, each) in nums.enumerated() {
            var numsDict: [Int:Int] = myDict[each, default: [:]]
            var posDict: [Int:Int] = myPos[each, default: [:]]
            let currentIndex = numsDict.count; numsDict[index] = currentIndex
            posDict[currentIndex] = index; myDict[each] = numsDict; myPos[each] = posDict
        }
 
        for (index, query) in queries.enumerated() { var minDistance = Int.max
            valueMatch: if let valueToMatch = nums[safe: query] {
                let idxDict = myDict[valueToMatch]!, posDict = myPos[valueToMatch]!
                guard idxDict.count > 1 else { break valueMatch }; var queryIndex = idxDict[query]!

                if (queryIndex - 1) >= 0 { 
                    calculateDistance(nums.count, query, posDict[(queryIndex - 1)]!, &minDistance)
                } else { calculateDistance(nums.count, query, posDict[(idxDict.count - 1)]!, &minDistance) }
                
                if (queryIndex + 1) < idxDict.count {
                    calculateDistance(nums.count, query, posDict[(queryIndex + 1)]!, &minDistance)
                } else { calculateDistance(nums.count, query, posDict[0]!, &minDistance) }
            }; minDistance != Int.max ? ans[index] = minDistance : nil
        }; return ans

        func calculateDistance(_ numsCount: Int, _ query: Int, _ each: Int, _ minDistance: inout Int) {
            let leftEdgeDistance = (nums.count - query) + each 
            let rightEdgeDistance = (nums.count - each) + query
            let edgeDistance = leftEdgeDistance < rightEdgeDistance ? leftEdgeDistance : rightEdgeDistance
            let normalDistance = (each - query) < 0 ? (-1) * (each - query) : (each - query)
            let distance = edgeDistance < normalDistance ? edgeDistance : normalDistance
            minDistance = minDistance < distance ? minDistance : distance
        }
    }
}

extension Collection {
    subscript(safe index: Index) -> Element? {
        indices.contains(index) ? self[index] : nil
    }
}

```
['Hash Table', 'Swift']