找到重复最多项

 

1.键值对法 (我最喜欢,简单易懂)

Array.prototype._RepeatMost = function () {
  let _this = [...this];
  const obj = {};
  let maxTerm = 1;
  let key = null;
  _this.forEach((item, index) => {
    if (obj[item] === undefined) {
      obj[item] = 1;
    } else {
      obj[item] = ++obj[item];
      if (maxTerm < obj[item]) {
        maxTerm = obj[item];
        key = item;
      }
    }
  })
  return { [key]: maxTerm }
}
console.log(ary._RepeatMost())

猜你喜欢

转载自www.cnblogs.com/MrZhujl/p/13174160.html
今日推荐