Skip to content

Instantly share code, notes, and snippets.

@RameshRM
Created February 4, 2023 19:28
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 RameshRM/4690adb292befc99277621f595a2390a to your computer and use it in GitHub Desktop.
Save RameshRM/4690adb292befc99277621f595a2390a to your computer and use it in GitHub Desktop.
function find(input) {
let prefix = input[0];
for (var i = 1; i < input.length; i++) {
let current = input[i];
if (current.length < prefix.length && prefix.startsWith(current)) {
prefix = current;
} else {
for (var j = 0; j < prefix.length; j++) {
if (current.charAt(j) === prefix.charAt(j)) {
continue;
} else {
prefix = prefix.substr(0, j);
break;
}
}
}
}
return (prefix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment