Code Lab

Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion

Count duplicate characters

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

Exemples

duplicateCounts('hello world')

Renvoie { l: 3, o: 2 }

duplicateCounts('1121')

Renvoie { '1': 3 }

duplicateCounts('abc')

Renvoie {}

Posé chez Amadeus Labs · Source

Chargement de l'éditeur...
  1. EntréeduplicateCounts('hello world')
    Attendu{ l: 3, o: 2 }
  2. EntréeduplicateCounts('banana')
    Attendu{ a: 3, n: 2 }
  3. EntréeduplicateCounts('abc')
    Attendu{}

Les tests cachés se lancent à la soumission