HackerRank: Minimum Distances (Easy)

Link to code challenge
function minimumDistances(a) { let obj = {}; let shortest = Infinity; for (let i = 0; i < a.length; i++) { if ( obj.hasOwnProperty(a[i]) ) { console.log('Obj property found'); let diff = Math.abs(obj[a[i]] - i); if (diff < shortest) shortest = diff; obj[a[i]] = i; } else obj[ a[i] ] = i; } if (shortest === Infinity) return -1; else return shortest; }

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int