我有一个字符串数组,必须从所有字符串中找到所有的公共字符



我有一个字符串数组。

let arr=["robin","rohit","roy"];

需要找到数组中所有字符串中存在的所有公共字符。

输出例如:r,o

我试着为上面的情况创建一个具有多个循环的函数,但我想知道实现它的有效方法是什么

这里有一个函数解决方案,它将使用任何可迭代值的数组(而不仅仅是字符串(,并使用对象身份比较来实现值相等:

function findCommon (iterA, iterB) {
const common = new Set();
const uniqueB = new Set(iterB);
for (const value of iterA) if (uniqueB.has(value)) common.add(value);
return common;
}
function findAllCommon (arrayOfIter) {
if (arrayOfIter.length === 0) return [];
let common = new Set(arrayOfIter[0]);
for (let i = 1; i < arrayOfIter.length; i += 1) {
common = findCommon(common, arrayOfIter[i]);
}
return [...common];
}
const arr = ['robin', 'rohit', 'roy'];
const result = findAllCommon(arr);
console.log(result);

const arr = ["roooooobin","rohit","roy"];
const commonChars = (arr) => {
const charsCount = arr.reduce((sum, word) => {
const wordChars = word.split('').reduce((ws, c) => {
ws[c] = 1;
return ws;
}, {});
Object.keys(wordChars).forEach((c) => {
sum[c] = (sum[c] || 0) + 1;
});
return sum;
}, {});
return Object.keys(charsCount).filter(key => charsCount[key] === arr.length);
}
console.log(commonChars(arr));

好吧,我们的想法是计算每个字母出现的次数,但每个字符串只计算1个字母

let arr=["robin","rohit","roy"];
function commonLetter(array){
var count={} //object used for counting letters total
for(let i=0;i<array.length;i++){
//looping through the array
const cache={} //same letters only counted once here
for(let j=0;j<array[i].length;j++){
//looping through the string
let letter=array[i][j]
if(cache[letter]!==true){
//if letter not yet counted in this string
cache[letter]=true //well now it is counted in this string
count[letter]=(count[letter]||0)+1
//I don't say count[letter]++ because count[letter] may not be defined yet, hence (count[letter]||0)
}
}
}
return Object.keys(count)
.filter(letter=>count[letter]===array.length)
.join(',')
}
//usage
console.log(commonLetter(arr))

无论你选择哪种方式,你仍然需要计算所有字符,据我所知,你无法绕过O(n*2)

arr=["robin","rohit","roy"];

let commonChars = sumCommonCharacters(arr);
function sumCommonCharacters(arr) {
data = {};
for(let i = 0; i < arr.length; i++) {
for(let char in arr[i]) {
let key = arr[i][char];
data[key] = (data[key] != null) ? data[key]+1 : 1;
}
}
return data;
}
console.log(commonChars);

如果有人对感兴趣,这里有一个1行

new Set(arr.map(d => [...d]).flat(Infinity).reduce((ac,d) => {(new RegExp(`(?:.*${d}.*){${arr.length}}`)).test(arr) && ac.push(d); return ac},[])) //{r,o}

您可以使用一个对象来检查每个字符的出现情况
循环数组中的单词,然后循环每个单词的字符。

let arr = ["robin","rohit","roy"];
const restWords = arr.slice(1);
const result = arr[0].split('').filter(char => 
restWords.every(word => word.includes(char)))
const uniqueChars = Array.from(new Set(result));
console.log(uniqueChars);

输入["abc","bcd","cbad"]

let arr = ["abcc", "bcd", "cbad"]
let arrLength = arr.length
let finalArr = arr.join("").split("").sort()
var obj = {};
var max = [];
for (var i = 0; i < finalArr.length; i++) {
(obj[finalArr[i]]) ? obj[finalArr[i]] += 1: obj[finalArr[i]] = 1;
if (obj[finalArr[i]] == arrLength) {
max.push(finalArr[i])
}
}
console.log(max)

输出["b","c"]

最新更新