题解 | 高频数据类型

高频数据类型

https://www.nowcoder.com/practice/687425f78096428baa58fbdcf024244a

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <script type="text/javascript">
      const _findMostType = (array) => {
        // 补全代码
        let map = new Map();
        let arr = [];
        array.forEach((item) => {
          let type = typeof item;
          map.set(type, (map.get(type) || 0) + 1);
        });

        let maxNum = Math.max(...map.values());
        for (let [key, value] of map) {
          if (value === maxNum) {
            arr.push(key);
          }
        }
        arr.push(maxNum)
        return JSON.stringify(arr);
      };
      console.log(
        _findMostType([1, "1", {}, {}, 2, 3, 4, 3, 3, "", "", "", "", ""])
      );
    </script>
  </body>
</html>

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务