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 nonscored) bonus points if you get a solution that works with these, though.
Solution Stats
Problem Comments

36 Comments
I don't understand why for test suite 4 :
a=[0 1 1 1 0 2 2 0 1 1 1 0];
y_correct = [1 1];
I expect y_correct = [1 1 1]
The idea seems to be to return the element that occurs in the longest run, or all such elements in case of a tie. In case 4 there are two longest runs, both with element 1.
Thanks Tim for the explanation . So as the vector [1 1 1] appears twice in test 4 with the unique value 1 , the result must be twice the unqiue value > [ 1 1]. ok ok thanks again
Nice
I have this array as I counted the times it repeats. However, can someone give me a command to pull out the max values with the number to the left? Any advise? The first value in column on the right top doesn't matter as it should always be 1. I try the max command it only shows the max value of 3...:
1 0
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Nice
Great problem. Thank you.
Good one
Nice problem
Is there a way to test output without submitting?
I really liked this one
This was fun.
The problem should specify that if the same number repeats the longest run of consecutive numbers more than once, it must be repeated in the output.
Great stuff!
I like this problem, it very fun
Should be more specific about the expected output, the solution checkers expect the output of numbers to be listed in the order of when they occurred in the sequence and also if the same number repeats in the sequence (with the largest run again) it needs to be repeated in the output. Both of these criteria broke my solution.
that was the first matlab problem i solved by myself, i'm so excited:)
Nice problem!
to be honest, though I solved the problem ,I didn't quite understand how to improve my code. I mean, the top of the solutions seemed quite strange to me .why by doing that the size would be smaller?
Hi 昱树 何,
In Matlab Cody, size of a solution is calculated by various factors, one of them being the number of variables used. However, the top solution in this problem (and in many others as well) exploit the use of regexp to gain a lower solution size. This was done because earlier the scoring was based on your solution size, which has now been changed to a fixed 10 points.
Hope this helps.
took a while doing this but overall good question. I got stuck in the last test case.
Good problem
hm, I don't think the assertion in test 4 is intuitively correct. The problem asks for the number(s) that have the longest consecutive repetitions in the input vector. So there can be multiple numbers having the longest runs (of equal size), but if all those numbers are the same, as in [1 1 2 1 1], surely the answer is "1", not "1 and 1".
I can only imagine that's what the implementation turned out to produce and instead of fixing the implementation to fit the problem, the tests was introduced to check for exactly that output ;)
Which is fine, you'll always have clients in real life that say they want something and when you roll it out they meant something different, it just bothers me that my nice solution doesn't work because of this 😂
The problem specifically states that column and row vectors must be supported, but then doesn't have a test for this. I'd suggest changing at least one of the tests to use a column vector.
Or maybe add one test where it is asserted that `longrun(a) == transpose(longrun(transpose(a)))`
Nice one
nice
Tricky!
Repeated consecutively makes the problem more difficult otherwise, we can use unique and histcounts to solve this quickly.
Not as difficult as it seems.
Managed to finally get the solution. Took some thought...
In my opinion a bad description of the task.
There is no information, that the order of apperance has to be correct and also not, that if a number comes more than one time with same occurace, you have to output them this times.
good problem
Test 7 is not correct. There are no *consecutively repeated* numbers in the sequence, as clearly stated in the task.
@Marcos the test is correct insofar as that "repeated consecutively" also covers the case where a number appears just once in a row.
test 6 ... y_correct = [3 2]' >> transpose...... is making it harder as i've check test7 now.. if i ouput transpose it'll crease itssues.. how could i do it!! Damn.. it's not a hard problem.. to start with.. if it was in python, c or C++ but here... it's getting clever.. man..*_*
function val=longrun(a)
% Given vector
% a = [1 2 2 2 1 3 2 1 4 5 1];
maxn = 1;
skip = 2;
i = 1;
sz = size(a);
if sz(1)==1
a = [a,max(a)+1];
elseif sz(2) == 1
a = [a;max(a)+1];
end
max1 = (max(a)+1);
while i < length(a)
for j = skip:length(a)
if a(j) ~= a(i)
skip = j+1;
% maxn = max(maxn, ji);
if (ji>maxn)
max1 = a(i);
elseif (ji==maxn)
if (i~=1)
max1 = [max1, a(i)];
else
max1 = a(i);
end
end
maxn = max(maxn, ji);
i = j;
break;
end
end
end
val = max1;
if sz(2) == 1
val = val'
end
end
Solution Comments
Show commentsProblem Recent Solvers5184
Suggested Problems

2962 Solvers

2158 Solvers

Number of 1s in a binary string
8440 Solvers

Create a vector whose elements depend on the previous element
632 Solvers

Try 1.5.4: Celsius to Fahrenheit
794 Solvers
More from this Author80
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!