Code Lab

Print how often every digit occurs

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

Examples

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

Prints 1: 2 | 0: 2 | 3: 1 | 5: 1

printDigitCounts([7, 7, 7])

Prints 7: 3

Asked at Capgemini · Source

Loading the editor...
  1. InputprintDigitCounts([ 1, 0, 0, 1, 3, 5 ])
    Output1: 2 0: 2 3: 1 5: 1
  2. InputprintDigitCounts([ 4, 2, 4 ])
    Output4: 2 2: 1

Hidden tests run on Submit.