2 つの文字列を受け取り、どちらかの文字列に含まれる文字を重複せずに表示する関数を作成します。
function characterOverlap(array1, array2) { let occurrence = {}; let str = array1.concat(array2); // find the count of each character Array.from(str).forEach((char) => { let currentCount = occurrence[char] || 0; occurrence[char] = currentCount + 1; }); // return the keys which is the individual character and join. const result = Object.keys(occurrence); return result.join(""); } console.log(characterOverlap("computer", "circuitemtop")); console.log(characterOverlap("frontend", "development")); console.log(characterOverlap("mivrog", "gormiv")); console.log(characterOverlap("praxet", "xetpar")); console.log(characterOverlap("stone", "tones")); console.log(characterOverlap("rescue", "secure"));
> computer > frontedvlpm > mivrog > praxet > stone > rescu
以上がJavaScript での文字と文字列の重複の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。