题解 | #高频数据类型#

高频数据类型

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

const _findMostType = (array) => {
        // 补全代码
        let typaArr = [];
        array.forEach((item, index) => {
          let currentType = typeof item;
          console.log(currentType);
          typaArr.push(currentType);
        });
        const countObj = typaArr.reduce((obj, newvalue) => {
          if (newvalue in obj) {
            obj[newvalue]++;
          } else {
            obj[newvalue] = 1;
          }
          return obj;
        }, {});
        let max = 0;
        let arr = [];
        for (let i in countObj) {
          if (countObj[i] > max) {
            max = countObj[i];
            arr = [i];
          } else if (countObj[i] === max) {
            arr.push(i);
          }
        }
        if(arr.length>0){
            arr.push(countObj[arr[0]])
        }
       return arr
      };

全部评论

相关推荐

牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务