Cumulative Distinct Count

EasyData EngineeringArrayMathPrefix Sum

Description

Given an array, return an array where each position holds the number of distinct values seen from the start up to and including that position.

Examples

Input:[1,2,2,3]
Output:[1,2,2,3]
Explanation:

Each position reports how many different values have appeared so far while scanning from the start.

Input:[5,5,5]
Output:[1,1,1]
Explanation:

Each position reports how many different values have appeared so far while scanning from the start.

Input:[1,2,3,4]
Output:[1,2,3,4]
Explanation:

Each position reports how many different values have appeared so far while scanning from the start.

Constraints

  • 1 ≤ length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.