📌  相关文章
📜  查找两个字符串中的共同字符 javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:05.994000             🧑  作者: Mango

代码示例3
let x = "farceus";
let y = "character";

const commonCharacters = function (string1, string2) {
  let duplicateCharacter = "";
  for (let i = 0; i < string1.length; i += 1) {
    if (duplicateCharacter.indexOf(string1[i]) === -1) {
      if (string2.indexOf(string1[i]) !== -1) {
        duplicateCharacter += string1[i];
      }
    }
  }
  return [...duplicateCharacter];
};

console.log(commonCharacters(x, y));