Code Lab

Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado

Count duplicate characters

MediaMap 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.

Ejemplos

duplicateCounts('hello world')

Devuelve { l: 3, o: 2 }

duplicateCounts('1121')

Devuelve { '1': 3 }

duplicateCounts('abc')

Devuelve {}

Preguntada en Amadeus Labs · Fuente

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

Las pruebas ocultas se ejecutan al enviar