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.

Given an integer array `nums`, return an array `result` of the same length where `result[i]` is the number of times the value `nums[i]` appears in `nums` from index `0` up to and including index `i`. In other words, `result[i]` is the running occurrence count of `nums[i]` at position `i`. The first time a value shows up it gets 1, its second appearance gets 2, and so on. Each distinct value has its own count. Negative values and zero are counted like any other value, and `x` and `-x` are different values. The output keeps the input order: `result[i]` belongs to position `i`. Example 1: ```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 for the 1st time, 1 for the 2nd time, 3 for the 1st time, 2 for the 2nd time, and 1 for the 3rd time. Example 2: ```text Input: nums = [-1,0,-1,0,0] Output: [1,1,2,2,3] ``` The second `-1` is its 2nd occurrence, and the three zeros get 1, 2 and 3. Constraints: - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` No input value or returned count exceeds 2^31-1, so a 32-bit `int` holds every value in Java and C++.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Every value in nums and every returned count (at most len(nums)) fits in a signed 32-bit integer

Examples

Input: ([1, 2, 1, 3, 2, 1],)

Expected Output: [1, 1, 2, 1, 2, 3]

Explanation: Source example 1: values 1, 2 and 3 interleave and each keeps its own running count.

Input: ([5, 5, 5, 5],)

Expected Output: [1, 2, 3, 4]

Explanation: Source example 2: one repeated value counts up from 1 to 4.

Hints

  1. The answer at index i depends only on the elements at indices 0 through i.
  2. Values can be negative or as large as 10^9, so a value cannot be used directly as a position in an array of length len(nums).
  3. Two different values never share a count, even when one is the negation of the other.

Running Occurrence Count at Each Position

easyCitadelSoftware Engineer

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

In other words, result[i] is the running occurrence count of nums[i] at position i. The first time a value shows up it gets 1, its second appearance gets 2, and so on. Each distinct value has its own count. Negative values and zero are counted like any other value, and x and -x are different values. The output keeps the input order: result[i] belongs to position i.

Example 1:

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 for the 1st time, 1 for the 2nd time, 3 for the 1st time, 2 for the 2nd time, and 1 for the 3rd time.

Example 2:

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

The second -1 is its 2nd occurrence, and the three zeros get 1, 2 and 3.

Constraints:

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

No input value or returned count exceeds 2^31-1, so a 32-bit int holds every value in Java and C++.

Examples

Example 1
Input
([1, 2, 1, 3, 2, 1],)
Output
[1, 1, 2, 1, 2, 3]
Notes
Source example 1: values 1, 2 and 3 interleave and each keeps its own running count.
Example 2
Input
([5, 5, 5, 5],)
Output
[1, 2, 3, 4]
Notes
Source example 2: one repeated value counts up from 1 to 4.

Constraints

  • •1 <= len(nums) <= 10^5
  • •-10^9 <= nums[i] <= 10^9
  • •Every value in nums and every returned count (at most len(nums)) fits in a signed 32-bit integer

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def running_frequency(nums: list[int]) -> list[int]:
Sample call
running_frequency([1, 2, 1, 3, 2, 1])
Language
Case 1
Input
([1, 2, 1, 3, 2, 1],)
Expected
[1, 1, 2, 1, 2, 3]
Case 2
Input
([5, 5, 5, 5],)
Expected
[1, 2, 3, 4]

Browse More Questions

Show the approach

Approach

Walk through nums from left to right while keeping a dictionary that maps each value to how many times it has appeared so far. At index i, add one to the tally of nums[i] and store the new tally in result[i].

Invariant: just before index i is processed, the tally of every value v equals the number of indices j < i with nums[j] == v. Adding one for nums[i] makes its tally count the indices j <= i, which is exactly the definition of result[i]. Tallies of other values are untouched, so the invariant holds for index i + 1.

The JavaScript reference reaches the same counts without a large Map: it sorts the indices by (value, index), then numbers each run of equal values 1, 2, 3 and so on in index order, writing each number back to its original position.

Edge cases: a single element gives [1]; n copies of one value give 1 through n; all distinct values give all ones; negative values and zero are ordinary dictionary entries, and x and -x are separate entries. Every count is at most len(nums) <= 10^5, so 32-bit integers suffice.

Time complexity:
O(n) expected (the sort-based JavaScript reference is O(n log n))
Space complexity:
O(n)