Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Input: strs = ["flower","flow","flight"]
Output: "fl"
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
const longestCommonPrefix = (strs) => {
let result = '';
let single = [];
let match = [];
let i = 0;
while (single.length === match.length) {
single = strs.map((str) => str[i]);
match = single.filter((ltr) => ltr === single[i]);
single.length === match.length && (result += match[0]);
i++;
}
return result;
};
const longestCommonPrefix = (strs) => {
if (strs.length === 0) return '';
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.substring(0, prefix.length - 1);
}
}
return prefix;
};