JS Coding Question #9: Get Max Character In A String [Challenging]

Interview Question #9:

Write a function that will return the max character in a string.? You may get variation to the question as well like Write a function that will return that most commonly used character in a sentence or similar.


This content originally appeared on DEV Community and was authored by Let's Code

Interview Question #9:

Write a function that will return the max character in a string.? You may get variation to the question as well like Write a function that will return that most commonly used character in a sentence or similar.

Additional Rules:

  1. Treat lowercase and uppercase the same
  2. Only count alphabetic characters, no symbols and numbers
  3. Return one max character in case of multiple max characters

If you need practice, try to solve this on your own without looking at the solution below.

Feel free to bookmark ? even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Codepen:

If you want to play around and experiment with the code: https://codepen.io/angelo_jin/pen/abwYGPo

Solution below will cycle on every string and create a map. Once the map is created, cycle on the map and use the variables created to see if the current char has greater count. Assign char and max count accordingly.

// Helper function to remove non alphabetic characters and transform string to lowercase
function normalizeString(str) {
  return str
    .replace(/[^\w]/g, '')
    .toLowerCase()
}

function getMaxChar(str) {
  const charMap = {}
  let max = 0
  let maxChar = ''

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

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

  return maxChar
}

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee

Video below if you prefer instead of bunch of text/code ??


This content originally appeared on DEV Community and was authored by Let's Code


Print Share Comment Cite Upload Translate Updates
APA

Let's Code | Sciencx (2021-09-22T22:20:04+00:00) JS Coding Question #9: Get Max Character In A String [Challenging]. Retrieved from https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/

MLA
" » JS Coding Question #9: Get Max Character In A String [Challenging]." Let's Code | Sciencx - Wednesday September 22, 2021, https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/
HARVARD
Let's Code | Sciencx Wednesday September 22, 2021 » JS Coding Question #9: Get Max Character In A String [Challenging]., viewed ,<https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/>
VANCOUVER
Let's Code | Sciencx - » JS Coding Question #9: Get Max Character In A String [Challenging]. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/
CHICAGO
" » JS Coding Question #9: Get Max Character In A String [Challenging]." Let's Code | Sciencx - Accessed . https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/
IEEE
" » JS Coding Question #9: Get Max Character In A String [Challenging]." Let's Code | Sciencx [Online]. Available: https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/. [Accessed: ]
rf:citation
» JS Coding Question #9: Get Max Character In A String [Challenging] | Let's Code | Sciencx | https://www.scien.cx/2021/09/22/js-coding-question-9-get-max-character-in-a-string-challenging/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.