occurences

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Forming Arrays from the Character Map
function maxRecurringChar(text) {
    let charMap = {}
    let charArray =[]
    let vaulesArray = []
    let maxCharValue = 0

    for (let char of text) {
        if (charMap.hasOwnProperty(char)) {
            charMap[char]++
        } else {
            charMap[char] = 1
        }
    }

    charArray = Object.keys(charMap)
    vaulesArray = Object.values(charMap)
    maxCharValue = Math.max(...vaulesArray)

    return charArray[vaulesArray.indexOf(maxCharValue)]
}
maxRecurringChar('aabacada') // will return 'a'
ready
For…in Iteration
function maxRecurringChar(text) {
    let charMap = {}
    let maxCharValue = 0
    let maxChar = ''

    for (let char of text) {
        if (charMap.hasOwnProperty(char)) {
            charMap[char]++
        } else {
            charMap[char] = 1
        }
    }

    for (let char in charMap) {
        if (charMap[char] > maxCharValue) {
            maxCharValue = charMap[char]
            maxChar = char
        }
    }

    return maxChar
}
maxRecurringChar('aabacada') // will return 'a'
ready
Moje
function maxRecurringChar(text) {
	let maxCharValue = 0
    let maxChar = ''
  const count_dict = [...text].reduce((acc, curr) => {
    acc[curr] = (acc[curr] || 0) + 1;
    return acc;
  }, {});
 for (let char in count_dict) {
        if (count_dict[char] > maxCharValue) {
            maxCharValue = count_dict[char]
            maxChar = char
        }
    }

    return maxChar
}
maxRecurringChar('aabacada') // will return 'a'
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.