g | x | w | all
Bytes Lang Time Link
010Husk240912T202928Zint 21h
008Uiua231012T183704Zchunes
012Uiua231012T223333ZDominic
019Haskell + hgl231001T013201ZWheat Wi
114TSQL230911T054631ZDanielOn
074Rust230914T022237ZSaNoy Sa
022julia230911T051800ZDamian P
nanScala 3230911T015533Z138 Aspe
032Perl 5230912T100452ZKjetil S
013Charcoal230911T142106ZNeil
061Google Sheets230912T052411Zdoubleun
037R230910T183509ZDominic
047Ruby230910T201656ZG B
093C clang 16230911T142124ZPeter Co
051Python230910T182749Zcorvus_1
051JavaScript ES6230910T185800ZArnauld
510Nibbles230910T223254ZDominic
065Excel230911T095557ZJos Wool
00605AB1E230911T065417ZKevin Cr
051Factor + math.extras230911T051358Zchunes
022J230910T230259ZJonah
007Nekomata230911T022103Zalephalp
4375Vyxal230910T230407ZJonathan
005Jelly230910T222647ZJonathan
045Python 3230910T212648Zxnor
032Haskell230910T213207Zxnor
006Jelly230910T204725Zovs
051Retina 0.8.2230910T185910ZNeil
007Vyxal G230910T182623ZMTN

Husk, 10 bytes

→Σ►f=1gẊ-O

Try it online!

Takes in a list and outputs one number - how many consequent values are in there.

Explanation:

          O # Sort the input list
        Ẋ-  # Subtract adjacent pairs
       g    # Group together equal adjacent differences 
    f=1     # Filter only the groups of 1s (the differences of the consequent numbers)
  ►         # Take the longest sublist
 Σ          # Sum
→          # Increment by 1

Uiua, 8 bytes

/↥⊜⧻.⍘⊚⊝

Try it!

Inspired by Dominic van Essen's Uiua answer.

/↥⊜⧻.⍘⊚⊝
       ⊝  # deduplicate
     ⍘⊚   # inverted where, i.e. how many times each element occurs
  ⊜⧻.     # lengths of contiguous equal elements
/↥        # maximum

Original answer:

Uiua, 18 bytes

+1/↥⊜⧻.=1≡/-◫2⊝⊏⍏.

Try it!

+1/↥⊜⧻.=1≡/-◫2⊝⊏⍏.
               ⊏⍏.  # sort
              ⊝     # deduplicate
            ◫2      # windows of size two
         ≡/-        # difference of each window
       =1           # change non-ones to zeros
    ⊜⧻.             # find length of each contiguous group of ones
  /↥                # maximum
+1                  # plus one

Uiua, 12 bytes

/↥⍘⊚-⇡⧻.⊝⊏⍏.

Try it!

/↥⍘⊚-⇡⧻.⊝⊏⍏.
         ⊏⍏.  # sort
        ⊝     # deduplicate
     ⇡⧻.      # copy & make series of same length
    -         # subtract
  ⍘⊚          # invert where
/↥            # maximum

Haskell + hgl, 19 bytes

l<ixe(fl~<(<P1)<fe)

Uses xnor's method.

20 bytes

Here's one that works online:

P1<l<ih eq1<δ<sr<nb

Attempt This Online!

Explanation

Reflection

Some things can be improved:

T-SQL, 114 bytes

SELECT MAX(c)FROM(SELECT COUNT(b)c FROM(SELECT a*(a+1)/2-SUM(a)OVER(ORDER BY a)b FROM @t GROUP BY a)X GROUP BY b)Y

dbfiddle

Readable version of the code:

DECLARE @t TABLE(i INT NOT NULL IDENTITY(1,1) --table with identity to emulate a 'list' (not counted in byte count)
                , a INT NOT NULL);

INSERT INTO @t (a) --Assigning the 'list' values (not counted in byte count)
VALUES(100)
    , (4)
    , (200)
    , (1)
    , (3)
    , (2);

SELECT MAX(c) --5. Determine the maximum count from the subquery
FROM(
    SELECT COUNT(*)c --4. Count how many rows make up each group
    FROM(
        --After this query it is simply a matter of determining which value of b appears the most times, and how many times that is.
        SELECT a*(a+1)/2-SUM(a) OVER(ORDER BY a)b /*2. Calculates the sum of values which are missing from the consecutive sequence 
                                                between 1 and a (both inclusive). 
                                                This utilises the default behaviour of the windowed function. This defaults to:
                                                SUM(a) OVER(ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
                                                Which is far more descriptive, but too verbose for golf :).*/
        FROM @t
        GROUP BY a --1. Removes duplicate occurences of a, as this is just noise anyway...
    )X --necessary alias
    GROUP BY b -- 3. Group by b as these are the groups we want to know the size of
)Y --necessary alias

This algorithm does the following:

In my opinion the interesting part of this is prescribing the unique identifier. Let's generalise the problem, starting with an arbitrary subset of the natural numbers.

$$X \subseteq \mathbb N_{\ne 0}$$

Consider the following binary relation

$$R = \{(a, b): |a - b| \leq 1, (a, b) \in X \times X\}$$ R is the set of all ordered pairs which are no more than 1 distance apart. Here the distance is taxicab or L1 or whatever you want to call it.

It's fairly easy to see that R is symmetric, and reflexive, but NOT transitive.

Take the example $$X = \{1, 2, 3, 5, 6, 7, 8\}$$ $$(1, 2) \in R , (2, 3) \in R , (1, 3) \notin R$$

You can't have three distinct natural numbers all within a distance of 1 from eachother.

We need to extend the definition of R so that it is transitive. We need to find the transitive closure of R.

$$R^{+} = R \cup \{(a, c): (a, b) \in R \wedge (b, c) \in R\}$$

Now we have an equivalence relation, and it properly describes the equivalence classes that underly this problem. However we still need to determine a label for each of these equivalence classes. Normally the smallest positive representitve of each equivalence class is chosen. This would be [1] and [5] in the example above. But these are just labels, as long as they are unique they could be anything. Let's make a function that sufficiently labels an element's equivalence class.

$$Label(i) = \sum_{k=1}^{i} k - \sum_{j\in X \wedge j\leq i}{j}$$

We can replace the first sum with the equivalent expression $$ \sum_{k=1}^{i} k = \frac{i(i + 1)}{2}$$

Thus, $$Label(i) = \frac{i(i + 1)}{2} - \sum_{j\in X \wedge j\leq i}{j}$$

Intuitively this is just the sum of everything 'missing' up to the current element.

$$Label(i) = \sum_{j \in \mathbb N_{\ne 0} \setminus X \wedge j \leq i} j$$

Now i am really interested, is there a generalisation of this? That is to say if we take the base relation R and redefined it:

$$R = \{(a, b): |a - b| \leq k, (a, b) \in X \times X\}, k \in \mathbb N_{\ne 0}$$

Would there be an equivalent intuitive 'label function' for any value of k?

Rust, 74 bytes

Try it online!

|a:&[u8]|(*a.iter().min().unwrap()..).take_while(|x|a.contains(x)).count()

Explanation:

let f = |a: &[u8]| {
    // create an iterator starting from the least element
    (*a.iter().min().unwrap()..)
        // take item if in list, stopping at the first item not in list
        .take_while(|x| a.contains(x))
        // get total number of items taken
        .count()
};

julia, 24 22 bytes

Edit: 2-byte improvement due to MarcMush

Same approach as most others here, counting how many times v can be intersected with v-1 before we get an empty list

!v=v>[]&&!(v∩.~-v)+1

Attempt This Online!

How?

!v =
  v > [] &&         # return 0 if v == [] and terminate, otherwise proceed
  !(v ∩ .~ -v) + 1  # recurse on the intersection of v with v + 1 and add 1 
                      to count recursion depth. Here, ~ is bitwise not, so
                      ~(-k) == k-1 for signed integers.

Scala 3, 74 73 70 bytes

Port of @xnor's Python answer in Scala.

Thanks to @corvus_192's comment and @Kjetil S's comment.


def f(l:Seq[Int]):Int=if(l.size<1)0 else 1+f(l.filter(l contains _+1))

Attempt This Online!

Perl 5, 32 bytes

sub f{@_?1+f(grep$_+1~~@_,@_):0}

Try it online!

Copied idea from others here. Return count of rounds of removal of ints n where n+1 dont exists before list is empty.

Charcoal, 23 19 13 bytes

IL⌈⪪⭆⊕⌈θ¬№θι1

Try it online! Link is to verbose version of code. Explanation: Port of @JonathanAllan's Jelly answer, with the "untruth" replaced by a map over range using 0 for present and 1 for absent as this lets me split on 1s to find the longest run of 0s. (I can't split on 0s as this will fail if there are 10 of the same value in the list.)

       θ        Input list
      ⌈         Maximum
     ⊕          Incremented
    ⭆           Map over implicit range and join
         №      Count of
           ι    Current value
          θ     In input list
        ¬       Logical Not
   ⪪        1   Split on `1`s
  ⌈             Take the longest run of `0`s
 L              Take the length
I               Cast to string
                Implicitly print

Previous 19-byte solution also works with negative integers:

I⌈Eθ⌈Eθ∧¬⁻…·ιλθ⊕⁻λι

Try it online! Link is to verbose version of code. Explanation: Creates inclusive ranges between all pairs of input elements and outputs the length of the longest range that is a subset of the input.

   θ                Input array
  E                 Map over elements
      θ             Input array
     E              Map over elements
          …·        Inclusive range from
            ι       Outer value to
             λ      Inner value
         ⁻          Remove elements found in
              θ     Input array
        ¬           Is empty
       ∧            Logical And
                 λ  Inner value
                ⁻   Minus
                  ι Outer value
               ⊕    Incremented
    ⌈               Take the maximum
 ⌈                  Take the maximum
I                   Cast to string
                    Implicitly print

Google Sheets, 61 bytes

=sort(max(len(split(join(,frequency(A:A,sequence(9^4))),0))))

Put the input in cells A1:A and the formula in B1.

Uses frequency() like the Excel answer. Does not require CSE.

R, 50 38 37 bytes

Edit: -1 bytes thanks to Giuseppe

\(x,y=rle(1:max(x)%in%x))max(y$l*y$v)

Attempt This Online!

Ruby, 48 47 bytes

->l{1.step.find{|z|l.all?{|x|[*x..x+z]-l!=[]}}}

Try it online!

C (clang 16), 93 bytes

Using C23 _BitInt(65536) as a clang extension in C89 for implicit int. Works for input integers in the range [0,65535], e.g. 16-bit unsigned (but it takes them as an array of int, with an int n length). It returns the count.

typedef _BitInt(1<<16)T;f(*p,n){T b=0;for(;n--;)b|=(T)1<<p[n];for(n=1;b&=b<<1;n++);return n;}

First loop: Add each input to a set implemented as a bitmap (bitset |= 1<<n). Second loop: treating that as a wide integer, count the longest run of set bit. (Shift and AND to remove the low bit from each run of bits, count iterations until the whole thing becomes zero.)

Godbolt including a test caller with the test cases from the question.

See Add two really big numbers re: _BitInt. Clang 16 supports very large widths: the current max is 8388608 bits for signed or unsigned, _BitInt(1<<23). So not enough to handle values up to INT_MAX, but we don't handle negative int values either.

And yes, shifting and ANDing a 64KiB bitmap as a wide integer takes a ridiculous amount of asm instructions since clang fully unrolls it. I used _BitInt(1<<8) while developing this to keep edit/compile/run cycles short. With 1<<16 width, clang16 -std=gnu89 -Os -Wall -Wno-implicit-int makes 24249 lines of asm for x86-64 (only a couple of them labels instead of instructions.) It also uses about 30472 + 128 (red zone) = 30600 bytes of stack space. (64Kib = 8KiB)

Ungolfed:

typedef _BitInt(1<<8) T;   // narrower for testing with small number inputs
/* int */ ungolfed(int *p, int n){
    T bitset = 0;
    for(;n--;){
        bitset |= (T)1<<p[n];   // cast needed *before* the shift
    }
    int retval=1;            // reuse n for this in the golfed version
    // https://stackoverflow.com/questions/3304705/finding-consecutive-bit-string-of-1-or-0
    while(bitset &= bitset<<1){ // doesn't count the iteration that clears the last bit, hence retval=1 to start
        retval++;
    }
    return retval;
}

With clang -fsanitize=undefined, it will reject out-of-bounds inputs, e.g. runtime error: shift exponent 200000 is too large for 65536-bit type 'T' (aka '_BitInt(65536)')

Python, 122 113 101 52 51 bytes

Port of Arnaulds answer

lambda a:max(map(g:=lambda v:v in a and-~g(v+1),a))

Attempt This Online!

Previous Answer

lambda a:max(len([*l])for b,l in groupby(r in a for r in range(max(a)+1))if b)
from itertools import*

Attempt This Online!

JavaScript (ES6), 51 bytes

a=>Math.max(...a.map(g=v=>a.includes(v)&&1+g(v+1)))

Try it online!

Commented

a =>                 // a[] = input array
Math.max(...         // return the maximum value in this:
  a.map(g = v =>     //   for each value v in a[],
                     //   using a recursive callback function g:
    a.includes(v) && //     if a[] includes v:
      1 +            //       increment the final result
      g(v + 1)       //       and do a recursive call with v + 1
  )                  //   end of map()
)                    // end of Math.max()

JavaScript (ES6), 56 bytes

This version was originally shorter (50 bytes), but invalid (as pointed out by Dominic van Essen). Now fixed at the cost of 6 bytes by adding p^v?...:0, which makes it uncompetitive.

Expects an Uint32Array.

a=>a.sort().map(p=v=>p^v?p+1^(p=v)?i=1:m+=++i>m:0,m=1)|m

Try it online!

Nibbles, 5.5 5 bytes (10 nibbles)

,|`.$`&+~$

Attempt This Online!

Port of ovs' Jelly answer.

,|`.$`&+~$    # full program
,|`.$`&+~$$   # with implicit arg:
  `.          # iterate while unique
    $         # starting with input:
       +~$    #   add 1 to every element
     `&       #   intersection with original list
 |            # now, filter this list-of-lists
          $   #   to include only non-empty lists
,             # and return its length

Excel, 65 bytes

=MAX(LEN(TEXTSPLIT(CONCAT(FREQUENCY(A1#,SEQUENCE(MAX(A1#)))),0)))

Input is spilled vertical array in A1#.

05AB1E, 6 bytes

ZLåγOà

Port of @JonathanAllan's Jelly answer, so make sure to upvote him as well!

Try it online or verify all test cases.

Explanation:

Z       # Push the maximum of the (implicit) input-list (without popping)
 L      # Pop and push a list in the range [1,max]
  å     # Check for each value whether it's in the input-list (1 if truthy; 0 if falsey)
   γ    # Group adjacent equal bits together
    O   # Sum each inner list
     à  # Pop and push the maximum
        # (which is output implicitly as result)

Since I was curious: a port of @ovs' Jelly answer is 8 bytes instead:

.ΓÐ<åÏ}g

Try it online or verify all test cases.

Explanation:

.Γ    }   # Continue until the list no longer changes, keeping all intermediate results
          # (excluding the initial value), using the (implicit) input-list
  Ð       #  Triplicate the current list
   <      #  Pop the top copy, and decrease all its values by 1
    å     #  Pop it and another list, and check for each value-1 whether it's in thelist
     Ï    #  Pop both again, and keep all values at the truthy positions
       g  # After the changes-loop: pop and push its length
          # (which is output implicitly as result)

Factor + math.extras, 51 bytes

[ sort members [ - -1 = ] max-monotonic-count 1 + ]

Attempt This Online!

Sort, uniquify, then count the longest run of two adjacent numbers differing by one.

J, 22 bytes

[:#[:+/ .*~^:a:~1=-//~

Attempt This Online!

We view this is a graph problem. Let's consider the example 10, 9, 8, 7, 6, 5 for the explanation:

J, port of ovs's answer, 21 20 bytes

1#@}.((e.>:)#[)^:a:~

Attempt This Online!

Nekomata, 7 bytes

ˡ{N→ᵈ:∩

Attempt This Online!

A port of @ovs's Jelly answer.

ˡ{N→ᵈ:∩
ˡ{          Loop until failure, and return the number of iterations
  N             Check if the list is nonempty
   →            Increment
    ᵈ:∩         Intersect with the input

Vyxal, 35 bitsv2, 4.375 bytes

ÞǔĠṠG

Try it Online!

How?

Port of my Jelly answer.

ÞǔĠṠG - list of positive integers     e.g. [4, 8, 3, 4, 2, 7, 4]
Þǔ    - unthruth                           [0, 1, 1, 1, 0, 0, 1, 1]
                                   ( i.e.:     2  3  4        7  8  )
  Ġ   - group runs of equal elements       [[0], [1, 1, 1], [0, 0], [1, 1]]
   Ṡ  - sums                               [0, 3, 0, 2]
    G - maximum                            3

Jelly, 5 bytes

ṬŒg§Ṁ

A monadic Link that accepts a list of positive integers and yields the length of the longest consecutive sequence.

Try it online!

How?

ṬŒg§Ṁ - Link: list of positive integers, A  e.g. [4, 8, 3, 4, 2, 7, 4]
Ṭ     - untruth {A}                              [0, 1, 1, 1, 0, 0, 1, 1]
                                         ( i.e.:     2  3  4        7  8  )
 Œg   - group runs of equal elements             [[0], [1, 1, 1], [0, 0], [1, 1]]
   §  - sums                                     [0, 3, 0, 2]
    Ṁ - maximum                                  3

Python 3, 45 bytes

f=lambda l:len(l)and-~f({*l}&{x-1for x in l})

Try it online!

Without using sets:

Python, 48 bytes

f=lambda l:l>[]and-~f([x for x in l if-~x in l])

Try it online!

Repeatedly filters l by only keeping elements x where x+1 is also in l, and counts how many repetitions until the list is empty.

Haskell, 32 bytes

f[]=0
f l=1+f[n|n<-l,elem(n+1)l]

Try it online!

Repeatedly filters l by only keeping elements n where n+1 is also in l, and counts how many repetitions until the list is empty.

Jelly, 6 bytes

f‘$ƬL’

Try it online!

   Ƭ    -- iterate until the result doesn't change, collecting intermediate value
f‘      -- keep the values that are still in the list when each value is incremented
    L   -- take the length (number of intermediate values, including the initial list and the empty list at the end)
     ‘  -- decrement

Retina 0.8.2, 51 bytes

$
, 
\d+
$*
D`1+, 
O`1+
(\G1+, |1\1)+
$#1¶
O#^`
1G`

Try it online! Link includes test cases. Explanation:

$
, 

Suffix a comma to the last entry too.

\d+
$*

Convert to unary.

D`1+, 

Deduplicate.

O`1+

Sort ascending.

(\G1+, |1\1)+
$#1¶

Count consecutive sequences. The first alternation is only possible at the start of the match, while the second alternation is only possible on subsequent repetitions of the capture group.

O#^`

Sort descending numerically.

1G`

Take the first (i.e. largest).

Vyxal G, 56 bitsv2, 7 bytes

ṗ's¯1=A;@

Try it Online!

Explanation

ṗ's¯1=A;@  '# Implicit input
ṗ'     ;   '# Filter the powerset by:
  s¯        #  Sort and get deltas
    1=A     #  All equal to 1?
        @   # Length of each inner list
            # Take the maximum
            # Implicit output