Last active
February 20, 2020 15:03
-
-
Save daubattu/28e536c9ddcf8aecc5aa88a67299e005 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function minimumDistances(a) { | |
let min; | |
const findOtherIndex = index => { | |
for (let i = index + 1; i < a.length; i++) { | |
if (a[i] === a[index]) { | |
return i; | |
} | |
} | |
return null; | |
} | |
for(let i = 0; i < a.length; i++) { | |
if (findOtherIndex(i)) { | |
if (!min) { | |
min = Math.abs(i - findOtherIndex(i)) | |
} else { | |
if (Math.abs(i - findOtherIndex(i)) < min) { | |
min = Math.abs(i - findOtherIndex(i)) | |
} | |
} | |
} | |
} | |
return min || -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment