📜  在两个字符串中查找常见字符 javascript - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:05.788000             🧑  作者: 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));