g | x | w | all
Bytes Lang Time Link
059JavaScript V8250428T032111ZSteve Be
043Ruby250427T164924ZJordan
030R211229T233010ZDominic
007Husk211229T224651ZNatte
045Factor211229T193641Zchunes
073Python 3191001T201940ZMatthew
009Japt h190930T121134ZShaggy
00605AB1E190930T090857ZDorian
nan120105T063002ZBlazer
005Jelly180803T105331Zlynn
055Julia 0.6180803T082010ZSundar R
034R180801T192729ZJayCe
009Japt180731T140310ZLuis fel
nan141011T180601Zedem
031Mathematica141011T141157ZMartin E
024Just for fun141011T144602ZOptimize
nan120114T214920ZWłodar
nan120105T164656Zagmcleod
nan120108T060332Zelssar
nan120107T163500ZGareth
nanSince I can't edit code120106T192837ZSamuel D
079Python120106T171627ZSteven R
087Python120106T092113ZAnte
021APL120105T070051ZDillon C
nan120105T193102Zmellamok
023J120105T172009ZJ B
023GolfScript120105T135711ZHoward
054Haskell120105T102948ZJ B
nan120105T062405ZJoanis

JavaScript (V8), 59 bytes

a=>a.reduce((n,c,i)=>c+1==a[i+1]?(++s>n?n=s:n):(s=1,n),s=1)

Try it online!

Ruby, 43 bytes

->a{a.chunk_while{_2-_1<2}.map(&:size).max}

Attempt This Online!

J-uby, 35 bytes

A port of the above.

:chunk_while+(~:-|:>&2)|:*&:+@|:max

Attempt This Online!

R, 30 bytes

x=scan();max(rle(x-seq(!x))$l)

Try it online!

Approach boringly copied from other answers, but different from (and slightly shorter than) JayCe's earlier R answer.

Husk, 7 bytes

▲mLgz-N

Try it online!

Explanation

▲mLgz-N
    z N  zip input and infinite list [1..]
      -  with '-'
   g     group adjacent equal values
 mL      map length
▲        maximum

Factor, 45 bytes

[ [ - -1 = ] monotonic-split longest length ]

Try it online!

Python 3, 73 bytes

def f(l):s=[n-i for i,n in enumerate(l)];return max(s.count(i)for i in s)

Try it online!

This works by subtracting the index from each number in the list and finding the greatest frequency in the list.

Japt -h, 9 bytes

í- ò¦ mÊn

Try it

05AB1E, 7 6 bytes

∞-γ€gZ

Try it online!

Thanks to @Kevin Cruijssen for -1 byte

Code:

∞       push endless list [1, 2, 3, 4, 5, ...]
-       subtract that from input
γ       group by equal elements
€g      push lengths of each group
Z       push max length
        implicitly print top of stack

Python

new:

def s(q):
    i = r = 1
    l = -1
    for n in q:
        if n == l + 1:
            i += 1
        elif i > r:
            r,i = i,1
        else:
            i=1
        l=n
 return i if i > r else r

old (buggy):

def s(q):
 i=r=1;l=-1
 for n in q:
  if n==l+1:i+=1
  elif i>r:r,i=i,1
  l=n
 return r

test case:

x = [1,2,3,50,56,58,60,61,90,100]
print x
print s(x)
>>> 3

edit: forgot the input numbers had to be in order. fixed edit2: fixed a problem when all the numbers are a sequence, eg 1,2,3,4,5,6,7,8,9,10, and it returned just '1' edit3: oops, didn't realize it wasn't code golf.

Jelly, 5 bytes

_JŒɠṀ

Try it online!

_J          Subtract [1,2,3…] from the input.
            This will turn a run like [48,49,50] into something like [45,45,45].
  Œɠ        Get group lengths. (“aaaabbc” → [4,2,1])
    Ṁ       Maximum.

Julia 0.6, 55 bytes

a->((d=diff(a))[d.!=1]=x=0;maximum(x=(x+y)y for y=d)+1)

Try it online!

Going for \$ O(n) \$,

82 bytes

a->(l=m=0;i=1;while i<length(a);a[i]==a[i+1]-1 ? l+=1 : l=1;l>m&&(m=l);i+=1;end;m)

Try it online!

which ungolfed is:

function f(a)
    l = m = 0
    i = 1
    while i < length(a)
        a[i] == a[i+1]-1 ? l+=1 : l=1
        if l > m
          m = l
        end
        i += 1
    end
    return m
end

R, 34 bytes

u=rle(1:99%in%scan());max(u$l*u$v)

Try it online!

This is a copy-paste of Micky T's answer to this challenge. Upvote him!

Japt, 9 bytes

äa è_¥1ÃÄ

Try it online!

Perl

my @o = (1,4,4,6,7,45,46,47,48,98);

sub r{
    $c=1;
    $l=pop@_;
    $m=1;
    while($n=pop@_){
        if($l-$n==1){++$c;$m=$c if $c>$m}
        else{$c=1}
        $l=$n
    }   
    $m  
}

print "MAX: " . r(@o);

Output: MAX: 4

Mathematica, 50 31 bytes

Max[Length/@Split[#,#2-#==1&]]&

This is an anonymous function. You can either give it a name, by assigning it to something, or you can just append @{6, 9, 50, 51, 52, 72, 81, 83, 90, 92} to use it straight away.

Here is how it works:

#2-#==1& is a nested anonymous function, which takes two arguments and returns True if the arguments are consecutive integers (and False for other integer pairs).

Split then partitions the resulting array into runs of elements, for which the above function returns True.

Now we map Length onto the result to figure out how many numbers each run contains, and select the maximum.

Thanks to David Carraher for eliminating large parts by using a test function in Split!

Just for fun, CJam, 24 bytes

l~]M\{:H(=+H}*;0a/:,$W=)

I know this is not valid as the language did not exist at the time of asking the question, but I thought I will give it a try.

How it works:

l~]M\                     "Read the input, convert to array, put an empty array M before it";
     {      }*            "For each pair of the array, run the code block";
      :H(                 "Assign the second number from the pair to H and decrement it";
         =+               "Check if it is equal to previous number and put result in M";
           H              "Put back the second number so as to be used in next iteration":
              ;0a         "Drop the trailing last number and put [0] on stack";
                 /        "Split M on [0]";
                  :,      "For each split element, replace it with its length";
                    $W=   "Sort the final array and take its last element";
                       )  "Increment this highest element from the array";

Try it online here

Java

public static int getConsecutiveCount(int[] A) {
    int r = 1, t = 1;
    boolean c = false;
    for (int i = 0; i < A.length; i++) {
        if (i + 1 < A.length) {
            if (A[i + 1] - A[i] == 1) {
                    t++;
                    c = true;
            } else {
                c = false;
            }
        } else {
            c = false;
        }
        if (t > r && !c) {
            r = t;
            t = 1;
        }
    }
    return r;
}

Ruby

I'm generally not that great at knowing more efficient methods. Might be some ruby standard lib things I'm missing.

def consec_count(arr)
  tg = 1
  cg = 1
  l = arr[0]
  arr.each do |v|
    if l + 1 == v
      cg += 1
      tg = cg if tg < cg
    else
      cg = 1
    end
    l = v
  end
  tg
end

Python

def s(n):
l=0
t=1
for i in range(0,9):
    if n[i]+1==n[i+1]:
        t+=1
    else:
        if l<t:
            l=t
        t=1
return l

Scala

def maxconsec(ln:List[Int]):Int=
{
    var (max,current,last)=(1,1,-1)
    for(n<-ln)
    {
        if(n==last+1)
        {
            current+=1;
            if(current>max)max=current
        }
        else
        {
            current=1
        }
        last=n
    }
    return max
}

I think this is O(n); is it possible to be more efficient? Can the OP elaborate on how efficiency is being measured here?

Usage: (you can test it on simplyscala.com)

maxconsec(List(6,9,50,51,52,72,81,83,90,92))

Since I can't edit code, I'll redefine the Scheme answer (Racket or not, portable and easier to read). Variables have descriptive names and there is only one recursion of the list while being error free (i think). Who cares about characters when you got efficiency?

(define (count-consecutive lst)
  (call-with-current-continuation
    (lambda (return)
      (define (test n)
        (unless (integer? n)
          (return (error "Not integer: " n))))
      (define (g best) (f best n (cdr lst)))
      (let ([prev (car lst)] [count 0]) (test prev)
        (let f ([best 0] [prev prev]
                [lst (cdr lst)])
          (if (null? lst) best
              (let ([n (car lst)]) (test n)
                (if (= (- n prev) 1)
                    (let ([count (+ count 1)])
                      (if (> count best) (g count)
                          (g best)))
                    (let ([count 0])
                      (g best))))))))))

Python, 79 characters

f=lambda l:max(map(len,''.join(' x'[a-b==1]for a,b in zip(l[1:],l)).split()))+1

Python, 87 characters

Recursive solution:

def s(l):
 n=1
 while n<len(l)and l[n]-l[0]==n:n+=1
 return max(n,s(l[n:])if l else 0)

Testing:

>>> s([1, 2, 33, 44, 55, 66, 77, 88, 98])
2
>>> s([1, 3, 23, 24, 30, 48, 49, 70, 80])
2
>>> s([6, 9, 50, 51, 52, 72, 81, 83, 92])
3

APL, 26 21 characters

1+⌈/+/^\9 9⍴0,1=-2-/⎕

Here's the 26-character solution:

i←⎕⋄⌈/{1++/^\⍵↓1=-2-/i}¨⍳9

I used Dyalog APL as my interpreter, and ⎕IO should be set to 0 for the 26-character version.

Example:

      1+⌈/+/^\9 9⍴0,1=-2-/⎕
⎕:
      6 7 51 51 53 51 54 55 56 55
3

This answer explains most of what I in the 26-character solution, but I might have to write a new one up for the 21-character version.

JavaScript (80 93 107)

for(n=prompt(c=m=1).split(' '),i=9;i;m=++c>m?c:m)c*=!(--n[i]-n[--i])|0;alert(m);

Demo: http://jsfiddle.net/QqfY4/3/

Edit 1: Replaced a with n[i] and b with n[i-1]. Converted Math.max to ternary if. Moved initialization statement into for(.

Edit 2: Reversed iteration direction to eliminate need for i++ by changing to --i in second n[--i]. Replaced i++ with part of if body. Changed condition to i to take advantage of ending at 0 = false. Hard-code starting value of 9 due to spec 10 random numbers.

J, 23

>:>./(+*[)/\.(}.=>:&}:)

Sample use:

   >:>./(+*[)/\.(}.=>:&}:) 1 2 33 44 55 66 77 88 98
2
   >:>./(+*[)/\.(}.=>:&}:) 1 3 23 24 30 48 49 70 80
2
   >:>./(+*[)/\.(}.=>:&}:) 6 9 50 51 52 72 81 83 92
3

GolfScript, 23 characters

~]1\{.@-(!@*).@}*;]$)p;

Test cases:

"1 2 33 44 55 66 77 88 98"  --> 2
"1 3 23 24 30 48 49 70 80"  --> 2
"6 9 50 51 52 72 81 83 92"  --> 3

Haskell, 54

import List
f=maximum.map length.group.zipWith(-)[1..]

Scheme/Racket

Here's a trivial answer in Scheme...

(define f
  (λ (l)
    (letrec ((g (λ (l M c p)
                  (cond ((null? l)
                         (max M c))
                        ((= (car l) (+ p 1))
                         (g (cdr l) M (+ c 1) (car l)))
                      (else (g (cdr l) (max M c) 1 (car l)))))))
      (g l 1 1 -2))))

(f '(1 2 33 44 55 66 77 88 98)) ;=> 2
(f '(1 3 23 24 30 48 49 70 80)) ;=> 2
(f '(6 9 50 51 52 72 81 83 92)) ;=> 3

where