Modo entrevista. Cronômetro de 15 min, sem dicas e sem autocompletar
Most and least frequent character
DifícilMap 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.
Exemplos
mostAndLeastFrequent('hello world')Retorna { most: 'l', least: 'h' }
mostAndLeastFrequent('abba')Retorna { most: 'a', least: 'a' }
mostAndLeastFrequent('')Retorna { most: null, least: null }
Cobrada em Cognizant · Fonte
Carregando o editor...
- Entrada
mostAndLeastFrequent('hello world')Esperado{ most: 'l', least: 'h' } - Entrada
mostAndLeastFrequent('')Esperado{ most: null, least: null }
Os testes ocultos rodam no envio