# 一个字符串出现最多的字母

if (str.length === 1) {
  return str;
}

const charObj = {};
for (let i of Object.keys(str)) {
  const current = str[i];
  if (charObj[current] === undefined) {
    charObj[current] = 1;
  } else {
    charObj[current]++;
  }
}
let maxChar = '',
  maxNum = 0;

for (let i of Object.keys(charObj)) {
  const current = charObj[i];
  if (current > maxNum) {
    maxNum = current;
    maxChar = i;
  }
}

console.log(maxChar, '=======', maxNum);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

简化版

const maxDuplication2 = (str: string) => {
  if (str.length === 1) {
    return str;
  }

  let maxChar = '',
    maxNum = 0;

  const charObj = {};
  for (let i of Object.keys(str)) {
    const current = str[i];
    if (charObj[current] === undefined) {
      charObj[current] = 1;
    } else {
      charObj[current]++;
    }

    if (charObj[current] > maxNum) {
      maxChar = current;
      maxNum = charObj[current];
    }
  }

  console.log(maxChar, '=======', maxNum);
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25