首页 > web前端 > js教程 > 正文

JavaScript 中字符与字符串重叠

Mary-Kate Olsen
发布: 2024-10-22 06:28:03
原创
724 人浏览过

Character overlap with strings in 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 中字符与字符串重叠的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板