JavaScript 中字符与字符串重叠
编写一个函数,它接受两个字符串并显示出现在任一字符串中的字符(不带双精度)。
解决方案
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 中字符与字符串重叠的详细内容,更多请关注www.sxiaw.com其它相关文章!