Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get most reapead 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];
}
 
PREVIOUS NEXT
Tagged: #reapead #aphpabet #js
ADD COMMENT
Topic
Name
3+3 =