Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Created September 5, 2021 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDobby/fb39a56801b77ccb9d6df4c7228ed835 to your computer and use it in GitHub Desktop.
Save ChrisDobby/fb39a56801b77ccb9d6df4c7228ed835 to your computer and use it in GitHub Desktop.
function longestPrefix(strings: string[]) {
const { 0: first, [strings.length - 1]: last } = [...strings].sort();
let prefixLength = -1;
for (let i = 0; i < Math.min(first.length, last.length); i++) {
if (first[i] === last[i]) {
prefixLength = i;
} else {
break;
}
}
return first.slice(0, prefixLength + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment