Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

How often every number occurs

MédiaMap and Set10 min

Write a function countFrequencies(numbers) that finds the frequency of every number in an array: how many times it occurs.

  • numbers is an array. Every element is an integer: it can be positive, negative or 0.
  • Return a Map that works as a counter: the key is the number, the value is how many times it occurs. Keys stay numbers, not strings.
  • Every number of the array is a key once, with a count of at least 1.
  • For an empty array return an empty Map.
  • Key order is not checked.

Exemplos

countFrequencies([1, 2, 2, 3, 3, 3])

Retorna Map(3) { 1 => 1, 2 => 2, 3 => 3 }

countFrequencies([-1, 0, -1])

Retorna Map(2) { -1 => 2, 0 => 1 }

countFrequencies([])

Retorna Map(0) {}

Cobrada em ITC Infotech · Fonte

Carregando o editor...
  1. EntradacountFrequencies([ 1, 2, 2, 3, 3, 3 ])
    EsperadoMap(3) { 1 => 1, 2 => 2, 3 => 3 }
  2. EntradacountFrequencies([ 0, 7, 0, 4 ])
    EsperadoMap(3) { 0 => 2, 7 => 1, 4 => 1 }

Os testes ocultos rodam no envio