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.
Constraints
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000
Test Case 1
Input:
1 2 3 1 4 2
Output:
3 3 -1 3 -1 3
Test Case 2
Input:
5 5 5 5
Output:
1 1 1 1
Examples
Example 1
Input: [1, 2, 3, 4]
Output: [-1, -1, -1, -1]
Example 2
Input: [1, 2, 3, 1, 4, 2]
Output: [3, 4, -1, 3, -1, 4]
Loading...
[1, 2, 3, 4]
[-1, -1, -1, -1]
Nearest Duplicate Distance - Practice - Prepverse 🎓