Skip to content

Instantly share code, notes, and snippets.

@darkcris1
Created January 3, 2021 11:12
Show Gist options
  • Save darkcris1/a76589549a016970d912ad1ce56f4b8d to your computer and use it in GitHub Desktop.
Save darkcris1/a76589549a016970d912ad1ce56f4b8d to your computer and use it in GitHub Desktop.
match a longprefix
function longPrefix(arr) {
let result = ''
if (arr.length === 0) return result
for (let i = 0; i < arr[0].length || 0; i++) {
for (let j = 1; j < arr.length; j++) {
if (arr[0][i] === arr[j][i]) {
if (j + 1 === arr.length) {
result += arr[0][i]
}
} else {
return result
}
}
}
return result
}
console.log(longPrefix(['outside', 'outdated'])) // out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment