Code Lab

Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion

How often every number occurs

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

Exemples

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

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

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

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

countFrequencies([])

Renvoie Map(0) {}

Posé chez ITC Infotech · Source

Chargement de l'éditeur...
  1. EntréecountFrequencies([ 1, 2, 2, 3, 3, 3 ])
    AttenduMap(3) { 1 => 1, 2 => 2, 3 => 3 }
  2. EntréecountFrequencies([ 0, 7, 0, 4 ])
    AttenduMap(3) { 0 => 2, 7 => 1, 4 => 1 }

Les tests cachés se lancent à la soumission