Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Count duplicate characters

MédiaMap and Set10 min

Write a function duplicateCounts(text) that finds the duplicate characters in text and says how many times each of them occurs.

  • text is a string. It can be empty. It has no tabs or line breaks.
  • Return a plain object: the key is a character, the value is how many times it occurs in text. Not a Map.
  • Put in only the characters that occur more than once.
  • Spaces are skipped. All other characters count: letters, digits, punctuation.
  • Case matters: 'a' and 'A' are different characters, so 'aA' gives {}.
  • If no character occurs twice, return an empty object {}.
  • Key order is not checked.

The object itself can be your counter: one key per character.

Exemplos

duplicateCounts('hello world')

Retorna { l: 3, o: 2 }

duplicateCounts('1121')

Retorna { '1': 3 }

duplicateCounts('abc')

Retorna {}

Cobrada em Amadeus Labs · Fonte

Carregando o editor...
  1. EntradaduplicateCounts('hello world')
    Esperado{ l: 3, o: 2 }
  2. EntradaduplicateCounts('banana')
    Esperado{ a: 3, n: 2 }
  3. EntradaduplicateCounts('abc')
    Esperado{}

Os testes ocultos rodam no envio