Problem 672. Longest run of consecutive numbers
Given a vector a, find the number(s) that is/are repeated consecutively most often. For example, if you have
a = [1 2 2 2 1 3 2 1 4 5 1]
The answer would be 2, because it shows up three consecutive times.
If your vector is a row vector, your output should be a row vector. If your input is a column vector, your output should be a column vector. You can assume there are no Inf or NaN in the input. Super (albeit non-scored) bonus points if you get a solution that works with these, though.
Solution Stats
Problem Comments
-
41 Comments
@Andy The problem posed is basically this: in a given vector, for each run of identical numbers, identify the length of said run; then remove all runs of less than the maximal length, and replace all remaining runs with a single occurrence of the number they repeat.
I finally tackled the last problem!
I’m so proud of myself! I know my programming skills still have room for improvement, but I just had to share this amazing feeling with everyone. What a fantastic moment!
Ultimately solved it. I know that reduced size of the code is rewarded on Cody, but I prioritized readability and code with comments that explains what is logically happening. I didn't rely on diff or vertcat because I wanted to understand the basics of what the code should be doing.
Solution Comments
Show commentsGroup

KR SA Joint & Collaborative Cody Event
- 35 Problems
- 2 Finishers
- Convert from Fahrenheit to Celsius
- Area of an equilateral triangle
- Find the sum of all the numbers of the input vector
- Function composition
- Sunday bonus
- Sum of odd numbers in a matrix
- Find the position of first minimum value in an integer array with numbers
- Sum of first n positive integers
- Determinant of a 3x3 Matrix
- Draw a '0' in a one matrix!
- Mersenne Primes
- Select every other element of a vector
- Find a Pythagorean triple
- Is this triangle right-angled?
- ~~~~~~~ WAVE ~~~~~~~~~
- Create an n-by-n null matrix and fill with ones certain positions
- Find max
- Calculate the derivative of a polynomial
- Extract part elements of matrix into one new matrix
- Triangle sequence
- 2048 Next Move
- Which doors are open?
- matrix zigzag
- Which values occur exactly three times?
- Binary numbers
- Counting Money
- Shuffle
- With apologies to William Blake
- MatCAT - Reconstruct X from Its X-rays
- Solve an ODE: equation B
- Longest run of consecutive numbers
- Vector push
- Five-dimensional maze
- The sliding puzzle: 3D
- One track five lanes
Problem Recent Solvers5857
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!