Skip to content

Instantly share code, notes, and snippets.

@postpostmodern
Created May 22, 2012 16:05
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 postpostmodern/2769980 to your computer and use it in GitHub Desktop.
Save postpostmodern/2769980 to your computer and use it in GitHub Desktop.
function longestCommonPrefix(arr) {
var prefix = '';
var firstWord = arr[0];
function inAll(letter, idx) {
for(var j=0; j<arr.length; j++) {
if(arr[j][idx] != letter) return false;
}
return true;
}
for(var i=0; i<firstWord.length-1; i++) {
if(inAll(firstWord[i], i)) prefix += firstWord[i];
}
return prefix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment