programming-examples/js/Array/Write a JavaScript function to find the longest common starting substring in a set of strings..js
2019-11-15 12:59:38 +01:00

12 lines
467 B
JavaScript

function longest_common_starting_substring(arr1){
var arr= arr1.concat().sort(),
a1= arr[0], a2= arr[arr.length-1], L= a1.length, i= 0;
while(i< L && a1.charAt(i)=== a2.charAt(i)) i++;
return a1.substring(0, i);
}
console.log(longest_common_starting_substring(['go', 'google']));
console.log(longest_common_starting_substring(['SQLInjection', 'SQLTutorial']));
console.log(longest_common_starting_substring(['abcd', '1234']));