Last active
April 29, 2021 18:41
-
-
Save ORESoftware/8a7634377bfc78e14285d2cf6b588080 to your computer and use it in GitHub Desktop.
lacuna stuff
This file contains 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
const list = ['a', 'b', 'c', 'c', 'd', 'a', 'b', 'a', 'a', 'd', 'b']; | |
const findMin = (char1: string, char2: string, arr: Array<string>) : Number => { | |
let char1Index = -1; | |
let char2Index = -1; | |
let min = Number.MAX_SAFE_INTEGER; | |
for (let i = 0; i < arr.length; i++) { | |
const el = arr[i]; | |
if (String(arr[i])[0] === String(char1)[0]) { | |
if (char2Index >= 0 && (i - char2Index < min)) { | |
min = i - char2Index | |
} | |
char1Index = i | |
console.log({char1Index, i}) | |
continue; | |
} | |
if (String(arr[i])[0] === String(char2)[0]) { | |
if (char1Index >= 0 && (i - char1Index < min)) { | |
min = i - char1Index | |
} | |
char2Index = i | |
console.log({char2Index, i}) | |
continue; | |
} | |
} | |
return min; | |
}; | |
console.log( | |
findMin('b','d',list) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment