Search for a command to run...
You are given an integer array A. For every element, find the distance to its nearest identical element (either left or right). If no duplicate exists for that element, return -1 for that position. Return the result array.
This simulates finding the nearest repeated event in logs or user actions.
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000
1 2 3 1 4 2
3 3 -1 3 -1 3
5 5 5 5
1 1 1 1
Example 1
[1, 2, 3, 4][-1, -1, -1, -1]Example 2
[1, 2, 3, 1, 4, 2][3, 4, -1, 3, -1, 4][1, 2, 3, 4]
[-1, -1, -1, -1]