Skip to content

Instantly share code, notes, and snippets.

@jsatk
Created December 15, 2014 18:27
Show Gist options
  • Select an option

  • Save jsatk/f721c613f363794c754b to your computer and use it in GitHub Desktop.

Select an option

Save jsatk/f721c613f363794c754b to your computer and use it in GitHub Desktop.
Finds the longest prefix in an array of strings
var longestPrefix = function (arr) {
var firstString = arr[0],
prefix = '';
for (var i = 0; i < firstString.length; i++) {
for (var j = 1; j < arr.length; j++) {
if (arr[j].charAt(i) !== firstString.charAt(i)) {
return prefix;
}
}
prefix += firstString.charAt(i);
}
return prefix;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment