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):
Aandaare the same letter, so'Aa'gives['a']. - Only the Latin letters
a-zcount. Digits, spaces, other characters and letters outsidea-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...
- Input
findDuplicateLetters('Programming')Expected[ 'r', 'g', 'm' ] - Input
findDuplicateLetters('Hello World')Expected[ 'l', 'o' ] - Input
findDuplicateLetters('abc')Expected[]
Hidden tests run on Submit