Run-Length Encoding
MediumData EngineeringArray
Description
Given an array, compress it with run-length encoding: return an array of [value, runLength] pairs, where each pair represents a maximal run of consecutive equal values.
Examples
Input:
[1,1,2,3,3,3]Output:
[[1,2],[2,1],[3,3]]Explanation:
Each run of equal consecutive values collapses into that value paired with how many times it repeats in a row.
Input:
[5]Output:
[[5,1]]Explanation:
Each run of equal consecutive values collapses into that value paired with how many times it repeats in a row.
Input:
[1,2,3]Output:
[[1,1],[2,1],[3,1]]Explanation:
Each run of equal consecutive values collapses into that value paired with how many times it repeats in a row.
Constraints
- •
0 ≤ array length ≤ 10⁴