Count Array Element Frequencies
Problem statement
Given an integer array values, count how many times each distinct value appears.
Return a two-dimensional integer array in which each row is [value, frequency]. Sort the rows by value in ascending order.
Function
countFrequencies(values: int[]) → int[][]Examples
Example 1
values = [4,2,4,3,2,4]return = [[2,2],[3,1],[4,3]]The value 2 appears twice, 3 appears once, and 4 appears three times.
Example 2
values = [-1,-1,0,5,0]return = [[-1,2],[0,2],[5,1]]Negative and zero values are counted normally, and rows remain ordered by value.
Example 3
values = [7]return = [[7,1]]The only value appears once.
Constraints
1 <= values.length <= 10^5.-10^9 <= values[i] <= 10^9.