Python3 || 14 lines,  bisect     || T/S: 87% / 72%
The tricky part is finding an efficient way to determine the distances in the edge cases in which the circularity of the list is an issue. My attempt is in the
The tricky part is finding an efficient way to determine the distances in the edge cases in which the circularity of the list is an issue. My attempt is in the three lines of code above the `return` statement.
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:

        n = len(nums)
        d, ans = defaultdict(list), []

        for i, num in enumerate(nums):
            d[num].append(i)

        for q in queries:
            idx = d[nums[q]]
            m = len(idx)

            if m == 1: 
                ans.append(-1)
                continue

            j = bisect_left(idx, q)
            mn, mx = sorted([abs(q-idx[j - 1]), abs(q - idx[(j + 1)%m])])
            ans.append(min(mn, n - mx))

        return ans
```
[https://leetcode.com/problems/closest-equal-element-queries/submissions/1576060012/](https://leetcode.com/problems/closest-equal-element-queries/submissions/1576060012/)

I could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(*M* + *Q*), in which *N* ~ `len(nums)`, *M* ~ the average count in `d.values()`, and *Q* ~ `len(queries)`.(The bisect method is technically log *M*, but because the lists in `d.values()` are disjoint and in total are no greater than *N*, the time is just *O*(*N*)...I could be wrong)
['Python3']