Code Lab

Count duplicate characters

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

Examples

duplicateCounts('hello world')

Returns { l: 3, o: 2 }

duplicateCounts('1121')

Returns { '1': 3 }

duplicateCounts('abc')

Returns {}

Asked at Amadeus Labs · Source

Loading the editor...
  1. InputduplicateCounts('hello world')
    Expected{ l: 3, o: 2 }
  2. InputduplicateCounts('banana')
    Expected{ a: 3, n: 2 }
  3. InputduplicateCounts('abc')
    Expected{}

Hidden tests run on Submit.