Получите большинство пожинателей Aphpabet JS

function maxChar(str) {
  const charMap = {};
  let max = 0;
  let maxChars = [];

  for (let char of str) {
    if (charMap[char]) {
      charMap[char]++;
    } else {
      charMap[char] = 1;
    }
  }

  for (let char in charMap) {
    if (charMap[char] >= max) {
      max = charMap[char];
      maxChars.push(char);
    }

    maxChars.forEach((char, i) => {
      if (charMap[char] < max) {
        maxChars.splice(i, 1);
      }
    });
  }

  // sort maxChars by alphabetical order
  maxChars.sort();
  return maxChars[0];
}
Lucky Lyrebird