Count Occurrences of Each Value in an Integer List, Sorted by Value

Read the full interview experience this question came from →

Quick Overview

A short coding question: given a list of integers with repeated values, return each distinct value together with the number of times it appears, sorted by value. It tests frequency counting, handling negative numbers, and producing a deterministic output order.

Count Occurrences of Each Value in an Integer List, Sorted by Value

Company: Apple

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

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 ```python 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** ```text 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** ```text Input: nums = [-5, 0, -5, 7] Output: [[-5, 2], [0, 1], [7, 1]] ``` Negative values sort before zero. **Example 3** ```text Input: nums = [4, 4, 4, 4] Output: [[4, 4]] ``` A single distinct value produces a single pair.

Overview: A short coding question: given a list of integers with repeated values, return each distinct value together with the number of times it appears, sorted by value. It tests frequency counting, handling negative numbers, and producing a deterministic output order.

Read the full Apple Data Scientist interview experience this question came from

Apple logo
Apple
May 2, 2026
hardData ScientistTechnical ScreenCoding & Algorithms
0
0

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.