Created
January 21, 2020 18:48
-
-
Save trinadhkoya/9a3d0b37cd2a2ad67e16fd3f4b73f533 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
var temp = [1, 2, 4, 5, 6, 8, 2, 7, 2]; | |
var encountered = []; | |
function f(a) { | |
let min = -1; | |
for (let i = 0; i < a.length; i++) { | |
let id = i + 1; | |
while (id !== 0) { | |
id = a.indexOf(a[i], id) + 1; | |
const diff = (id - 1) - i; | |
min = (diff >= 0 && (diff < min || min === -1)) ? diff : min; | |
} | |
} | |
return min; | |
} | |
console.log(f(temp)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment