Jumping forward, backward, or teleporting to identical values: the crucial optimization is clearing the value-bucket immediately after its first expansion to prevent $O(n^2)$ duplicate scans.
map.get(arr[u]) is traversed, it MUST be cleared (.clear()). All matching indices have entered the queue at distance steps + 1; re-scanning them wastes $O(n)$ time.
"The array indices $0 \dots n-1$ are vertices. From index $i$, directed unit-weight edges point to $i - 1$, $i + 1$, and all $j$ sharing the same value $arr[j] == arr[i]$."
"The trap is $O(n^2)$ TLE when many elements share identical values. After expanding map.get(arr[i]), we must invoke .clear()! Re-scanning this bucket later yields no shorter distance."
"Because every jump type has unit cost 1, standard level-order BFS finds the minimal jump count to index $n-1$ on first arrival."
"With bucket eviction, each index is added to the queue at most once, and each value bucket is scanned exactly once. Time is strictly $O(n)$, space is $O(n)$."