To find the longest common prefix among an array of strings, compare characters from each string in the array.
- Initialize the first string in the array as the common prefix.
- Iterate through the rest of the strings, updating the common prefix by comparing it with each string.
- If no common prefix is found, return an empty string.
- Return the final common prefix.
function findCommonPrefix(arr) {
if (arr.length === 0) return "";
let prefix = arr[0];
for (let i = 1; i < arr.length; i++) {
while (arr[i].indexOf(prefix) !== 0) {
prefix = prefix.slice(0, prefix.length - 1);
if (prefix === "") return "";
}
}
return prefix;
}
// Example usage
console.log(findCommonPrefix(["flower", "flow", "flight"])); // Output: "fl"
console.log(findCommonPrefix(["dog", "racecar", "car"])); // Output: ""
This method has a time complexity of O(n * m), where n is the length of the array and m is the length of the longest string.
Tags: intermediate, JavaScript, Arrays, Algorithm