How often every number occurs
MediumMap and Set10 min
Write a function countFrequencies(numbers) that finds the frequency of every number in an array: how many times it occurs.
numbersis an array. Every element is an integer: it can be positive, negative or0.- 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.
Examples
countFrequencies([1, 2, 2, 3, 3, 3])Returns Map(3) { 1 => 1, 2 => 2, 3 => 3 }
countFrequencies([-1, 0, -1])Returns Map(2) { -1 => 2, 0 => 1 }
countFrequencies([])Returns Map(0) {}
Asked at ITC Infotech · Source
Loading the editor...
- Input
countFrequencies([ 1, 2, 2, 3, 3, 3 ])ExpectedMap(3) { 1 => 1, 2 => 2, 3 => 3 } - Input
countFrequencies([ 0, 7, 0, 4 ])ExpectedMap(3) { 0 => 2, 7 => 1, 4 => 1 }
Hidden tests run on Submit