easy +8 pts

Longest Run of Equal Values

Find the length of the longest consecutive sequence of identical values in a list.

Write a function `longest_run(values)` that takes a list of values (integers, floats, strings, etc.) and returns the length of the longest consecutive run of equal values. A run is a contiguous sub-list where every element is equal. If the list is empty, return 0. The function must handle any comparable values and runs of any length.

Constraints

- 0 <= len(values) <= 10^6 - Values can be any hashable Python object (int, float, str, bool, None, tuple). - Time: O(n), Space: O(1) auxiliary.

Example

```python
>>> longest_run([1, 2, 2, 3])
2
>>> longest_run([])
0
>>> longest_run([5])
1
>>> longest_run(['a', 'a', 'b', 'b', 'b'])
3
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Initialize two variables: current run length and best run length.
Iterate through the list, comparing each element to the previous one.
When the value changes, update the best run length and reset the current run length.
If the list is empty, return 0 before iterating.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.