Code Lab

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

Print how often every digit occurs

MediaMap and Set10 min

Write a function printDigitCounts(digits) that prints how many times every digit occurs in an array.

  • digits is an array of whole numbers from 0 to 9. It can be empty.
  • Print one line per digit with console log, in the format <digit>: <count>, for example 1: 2.
  • Print the lines after you have counted everything, one line per digit, not one line per array element.
  • The lines go in the order in which each digit first appears in the array: its insertion order.
  • For an empty array print nothing.
  • The function returns nothing.
  • The interviewer asked you to count with a Map. The tests check only the output; keep to the rule yourself.

Ejemplos

printDigitCounts([1, 0, 0, 1, 3, 5])

Imprime 1: 2 | 0: 2 | 3: 1 | 5: 1

printDigitCounts([7, 7, 7])

Imprime 7: 3

Preguntada en Capgemini · Fuente

Cargando el editor…
  1. EntradaprintDigitCounts([ 1, 0, 0, 1, 3, 5 ])
    Salida1: 2 0: 2 3: 1 5: 1
  2. EntradaprintDigitCounts([ 4, 2, 4 ])
    Salida4: 2 2: 1

Las pruebas ocultas se ejecutan al enviar