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
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.
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
- The answer at index i depends only on the elements at indices 0 through i.
- 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).
- Two different values never share a count, even when one is the negation of the other.