找出数组中出现次数最多的元素,这个解法有 bug

var each = [].forEach;
function search(arr) {
  var counts = {};

  each.call(arr, val => {
    if (counts[val] === void 0) {
      counts[val] = 0;
    }
    counts[val]++;
  });

  var maxCount = counts[arr[0]];
  var maxNumber = 0;

  each.call(arr, val => {
    if (counts[val] > maxCount) {
      maxCount = counts[val];
    }
  });

  each.call(arr, val => {
    if (counts[val] === maxCount) {
      maxNumber = val;
    }
  });

  return maxNumber;
}

Windows 上有 xshell 工具可以做收藏,但是 Mac 配置后可能更加方便和强大,网上有很多这方面的文章,这里自己也记录一下,方便以后查阅。下面介绍 2 种方式:

  1. sshpass
  2. expect

- 阅读剩余部分 -