Last Updated: June 7, 2026
Count how often each distinct value appears in nums and return those counts as [value, count] rows. Two details matter here.
The first is ordering. Rows must follow first-appearance order while scanning left to right, not numeric order and not the order a hash structure stores keys. For [9, 8, 9, 8, 7], 9 appears before 8 before 7, so the rows come out as [[9, 2], [8, 2], [7, 1]].
The second is duplicates. Every repeat adds to a value's count, but the value still produces only one row. The first occurrence fixes its position in the output. Every later occurrence only increments its count.
The empty array is a clean base case. With no elements there are no distinct values, so the answer is an empty array.
[value, count] row. Only the first occurrence fixes its position in the output.nums is empty, return an empty array. No special handling beyond letting the loop run zero times.Without any extra structure, scan the array and, for each value, count how many times it appears. To avoid producing two rows for a duplicated value, process a value only at its first occurrence. Before counting, look back at everything to the left. If the current value appeared earlier, skip it. If it is new, scan from the current position to the end and tally every match.
Acting on each value at its first occurrence makes the rows come out in first-appearance order.
i from 0 to n - 1:nums[i] appeared at any earlier index j < i. If it did, skip this index.i to the end and count how many elements equal nums[i].[nums[i], count] to the result.Input:
At i = 0, the value is 1. Nothing lies to its left, so it is new. Scanning from index 0, only one 1 exists, so append [1, 1]. At i = 1, the value is 2. It has not appeared before, so it is new. Scanning from index 1, there are two 2s, so append [2, 2]. At i = 2, the value is 2 again, but a 2 already appeared at index 1, so skip it. At i = 3, the value is 3. It is new, and only one 3 exists, so append [3, 1].
The repeated scanning is what makes this quadratic. Each value is checked against the part of the array before it and then counted across the part after it. A single pass that remembers counts as it goes removes both inner loops.
The nested loop rescans because it has no memory of what it has seen. A hash map fixes that. Walk through nums once, incrementing a counter for each value in the map. By the end, the map holds the exact frequency of every distinct value.
The map solves counting, but not ordering. A hash map does not guarantee keys come back in first-appearance order, and in several languages the iteration order is unspecified or randomized. To keep the output deterministic, maintain a separate list that records each value the first time it is seen. Walk that list and read each value's count from the map to build the result rows.
order.v in nums:v is not already in the map, append it to order.v.v in order, append [v, map[v]] to the result.Input:
Read 9: not in the map, so add it to order, then set its count to 1. Read 8: new, add to order, count 1. Read 9: already known, increment its count to 2. Read 8: already known, increment its count to 2. Read 7: new, add to order, count 1. The order list is [9, 8, 7] and the map is {9: 2, 8: 2, 7: 1}. Walking order produces the rows in first-appearance order.