Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Longest Consecutive Sequence of the Same Digit

Basic Acceptance 84.75% Points 10

You are given a non-negative integer n. Your task is to return the length of the longest run of identical consecutive digits in the number.

A run is a sequence of two or more identical digits appearing consecutively. If no digit repeats consecutively, the longest run has a length of 1.

Examples
Example 1

Input: n = 1112233
Output: 3
Explanation: The digit 1 appears three times consecutively, forming the longest run of length 3.

Example 2

Input: n = 4455566
Output: 3
Explanation: The digit 5 appears three times consecutively, forming the longest run of length 3.

Example 3

Input: n = 1234
Output: 1
Explanation: No digit repeats consecutively, so the longest run has a length of 1.

Constraints
  • 0 <= n <= 1018
Topics
Data Structures Basics Array String Searching Algorithms
Companies
Amazon ServiceNow Morgan Stanly
Added 31 Oct 2026 84.75% acceptance
Ln 1, Col 1Sign in to save your code
1112233
3