if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance
b . set ( x , Math . min ( d , b . get ( x ) ) ) ; } } let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let x = a [ i ] ;