Most and least frequent character
HardMap and Set15 min
Write a function mostAndLeastFrequent(text) that finds the character that occurs most often in a string and the character that occurs least often.
textis a string. It can be empty.- Whitespace (spaces, tabs, line breaks) is not counted. Letters, digits and punctuation are counted.
- Case matters:
'a'and'A'are different characters. - Return an object
{ most, least }:mostis the character with the highest frequency,leastthe character with the lowest frequency. - If several characters have the same frequency, the one that first appears earlier in the text wins.
- If there are no characters to count (empty string or only whitespace), return
{ most: null, least: null }. Use null, notundefined. - You can count with a map.
Examples
mostAndLeastFrequent('hello world')Returns { most: 'l', least: 'h' }
mostAndLeastFrequent('abba')Returns { most: 'a', least: 'a' }
mostAndLeastFrequent('')Returns { most: null, least: null }
Asked at Cognizant · Source
Loading the editor...
- Input
mostAndLeastFrequent('hello world')Expected{ most: 'l', least: 'h' } - Input
mostAndLeastFrequent('')Expected{ most: null, least: null }
Hidden tests run on Submit