Code Lab

Duplicate letters in a string

MediumMap and Set10 min

Write a function findDuplicateLetters(text) that finds the duplicate letters in text: the letters that occur more than once.

  • Return an array of letters in lower case.
  • Case does not matter (case insensitive): A and a are the same letter, so 'Aa' gives ['a'].
  • Only the Latin letters a-z count. Digits, spaces, other characters and letters outside a-z (like é) are ignored.
  • Every duplicate letter is in the result once, even if it occurs three or more times.
  • Order: by the first occurrence of the letter in text. 'Programming' gives ['r', 'g', 'm'], not ['r', 'm', 'g'].
  • If there are no duplicate letters (also for an empty string), return an empty array.

A simple way is a counter for every letter in a Map, then a second pass over it.

Examples

findDuplicateLetters('Programming')

Returns [ 'r', 'g', 'm' ]

findDuplicateLetters('Hello World')

Returns [ 'l', 'o' ]

findDuplicateLetters('abc')

Returns []

Asked at Amazon · Source

Loading the editor...
  1. InputfindDuplicateLetters('Programming')
    Expected[ 'r', 'g', 'm' ]
  2. InputfindDuplicateLetters('Hello World')
    Expected[ 'l', 'o' ]
  3. InputfindDuplicateLetters('abc')
    Expected[]

Hidden tests run on Submit.