Created
March 17, 2020 07:08
-
-
Save toxi-kb/859037d3c6cbe2736ca441837029edfc 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(numbers) { | |
| const numberQuantity = numbers.length; | |
| const firstOccurrences = {}; | |
| let minDistance = Infinity; | |
| for (let idx = 0; idx < numberQuantity; idx += 1) { | |
| const num = numbers[idx]; | |
| const firstOccurrenceIdx = firstOccurrences[num]; | |
| if (firstOccurrenceIdx === undefined) { | |
| firstOccurrences[num] = idx; | |
| } else if (idx - firstOccurrenceIdx < minDistance) { | |
| minDistance = idx - firstOccurrenceIdx; | |
| } | |
| } | |
| return minDistance !== Infinity ? minDistance : -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment