AlgoMaster Logo

Count Frequency of Each Element

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

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.

Key Constraints:

  • First-appearance ordering → Rows follow the order each value is first encountered scanning left to right, not sorted order. You need a way to remember that order separately from the counts.
  • Duplicates collapse into one row → A value that appears many times still yields a single [value, count] row. Only the first occurrence fixes its position in the output.
  • Empty array → When nums is empty, return an empty array. No special handling beyond letting the loop run zero times.

Approach 1: Nested Loop

Intuition

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.

Algorithm

  1. Create an empty result list.
  2. For each index i from 0 to n - 1:
    • Check whether nums[i] appeared at any earlier index j < i. If it did, skip this index.
    • Otherwise, scan from i to the end and count how many elements equal nums[i].
    • Append [nums[i], count] to the result.
  3. Return the result.

Example Walkthrough

Input:

0
1
1
2
2
2
3
3
nums

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

0
[1,1]
1
[2,2]
2
[3,1]
result

Code

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.

Approach 2: Hash Map

Intuition

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.

Algorithm

  1. Create an empty hash map from value to count and an empty list order.
  2. For each value v in nums:
    • If v is not already in the map, append it to order.
    • Increment the map entry for v.
  3. For each value v in order, append [v, map[v]] to the result.
  4. Return the result.

Example Walkthrough

Input:

0
9
1
8
2
9
3
8
4
7
nums

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.

0
[9,2]
1
[8,2]
2
[7,1]
result

Code