Given a list of integers that may contain repeated values, count how many times each distinct value appears.
The report describes the task only as counting the occurrences of each number in a list with duplicates; this version fixes the output format so that the answer is unique.
Function Signature
def count_occurrences(nums: list[int]) -> list[list[int]]:
Rules
-
Return one pair
[value, count]
for every distinct value in
nums
, where
count
is the number of times
value
appears in
nums
.
-
Sort the pairs by
value
in ascending numeric order.
-
Each distinct value appears in exactly one pair, so the counts add up to
len(nums)
.
Constraints
-
1 <= len(nums) <= 10^5
-
-10^9 <= nums[i] <= 10^9
; values may be negative, zero or repeated.
-
Every value and every count fits in a 32-bit signed integer.
Examples
Example 1
Input: nums = [3, 1, 3, 2, 1, 3]
Output: [[1, 2], [2, 1], [3, 3]]
The value 1 appears twice, 2 once and 3 three times.
Example 2
Input: nums = [-5, 0, -5, 7]
Output: [[-5, 2], [0, 1], [7, 1]]
Negative values sort before zero.
Example 3
Input: nums = [4, 4, 4, 4]
Output: [[4, 4]]
A single distinct value produces a single pair.