Basic numeric runs
Input
[1, 1, 2, 2, 2, 3]
Output
[[1, 1], [2, 2, 2], [3]]
Two 1s form the first run, three 2s the second, and a single 3 the last.
Full lesson preview
Group runs of consecutive equal values in a list into sublists, preserving order and element types.
Problem statement
Task
Examples
Input
[1, 1, 2, 2, 2, 3]
Output
[[1, 1], [2, 2, 2], [3]]
Two 1s form the first run, three 2s the second, and a single 3 the last.
Input
[True, True, False, False, False]
Output
[[True, True], [False, False, False]]
Runs are formed using == equality; booleans group separately from other values.
Input format
Output format
Constraints
Samples
Input
["a", "a", "b", "b", "a"]
Output
[['a', 'a'], ['b', 'b'], ['a']]
The last 'a' is not consecutive with the first two, so it starts a new run.