Code Lab

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

Duplicate letters in a string

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

Ejemplos

findDuplicateLetters('Programming')

Devuelve [ 'r', 'g', 'm' ]

findDuplicateLetters('Hello World')

Devuelve [ 'l', 'o' ]

findDuplicateLetters('abc')

Devuelve []

Preguntada en Amazon · Fuente

Cargando el editor…
  1. EntradafindDuplicateLetters('Programming')
    Esperado[ 'r', 'g', 'm' ]
  2. EntradafindDuplicateLetters('Hello World')
    Esperado[ 'l', 'o' ]
  3. EntradafindDuplicateLetters('abc')
    Esperado[]

Las pruebas ocultas se ejecutan al enviar