Count how many times each array value has appeared so far at every index

Quick Overview

Given an integer array, return for each index how many times that index's value has appeared from the start of the array through that position. It tests one-pass counting with a hash map and keeping a running count for every distinct value.

Count how many times each array value has appeared so far at every index

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, return an array `result` of the same length where `result[i]` is the number of times `nums[i]` has appeared in `nums` from index `0` up to and including index `i`. In other words, `result[i]` is the running occurrence count of the value `nums[i]` at position `i`. ### Function Signature ```python def running_frequency(nums: list[int]) -> list[int]: ... ``` ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1,2,1,3,2,1] Output: [1,1,2,1,2,3] ``` At each index: 1 appears for the 1st time, 2 appears for the 1st time, 1 appears for the 2nd time, 3 appears for the 1st time, 2 appears for the 2nd time, and 1 appears for the 3rd time. ```text Input: nums = [5,5,5,5] Output: [1,2,3,4] ``` 5 occurs for the 1st, 2nd, 3rd and 4th time at the four positions. ```text Input: nums = [-1,0,-1,0,0] Output: [1,1,2,2,3] ``` Negative values and zero are counted like any other value. The second `-1` is its 2nd occurrence, and the three zeros get 1, 2 and 3.

Overview: Given an integer array, return for each index how many times that index's value has appeared from the start of the array through that position. It tests one-pass counting with a hash map and keeping a running count for every distinct value.

Citadel logo
Citadel
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, return an array result of the same length where result[i] is the number of times nums[i] has appeared in nums from index 0 up to and including index i.

In other words, result[i] is the running occurrence count of the value nums[i] at position i.

Function Signature

def running_frequency(nums: list[int]) -> list[int]:
    ...

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Input: nums = [1,2,1,3,2,1]
Output: [1,1,2,1,2,3]

At each index: 1 appears for the 1st time, 2 appears for the 1st time, 1 appears for the 2nd time, 3 appears for the 1st time, 2 appears for the 2nd time, and 1 appears for the 3rd time.

Input: nums = [5,5,5,5]
Output: [1,2,3,4]

5 occurs for the 1st, 2nd, 3rd and 4th time at the four positions.

Input: nums = [-1,0,-1,0,0]
Output: [1,1,2,2,3]

Negative values and zero are counted like any other value. The second -1 is its 2nd occurrence, and the three zeros get 1, 2 and 3.