g | x | w | all
Bytes Lang Time Link
045Wolfram Language Mathematica220130T031059Zatt
009Husk220130T093308ZDominic
01105AB1E220131T091509ZKevin Cr
038Charcoal220128T195932ZNeil
080Haskell220130T102101Zovs
083Haskell220130T002019ZAZTECCO
077Factor + math.unicode220130T045540Zchunes
010Brachylog220128T173450ZDLosc
162R220129T201349Zpajonk
028Burlesque220129T114522ZDeathInc
063Ruby220129T083929ZG B
011Jelly220128T173112Zhyperneu
064JavaScript Node.js220129T013033Ztsh
010Jelly220128T223002ZJonathan
078Retina 0.8.2220128T185223ZNeil
075JavaScript Node.js220128T182020Zl4m2
068Python 3220128T180419ZAnttiP
014Japt h220128T173359ZShaggy

Wolfram Language (Mathematica), 48 47 45 bytes

Last@*Select[1##&@@Counts@#<3&]@*Subsequences

Try it online!

Input [list]. Output a list.


Wolfram Language (Mathematica), 45 bytes

f[s=___,Longest@a:s,s]/;Times@@Counts@{a}<3=a

Try it online!

Input [list...]. Output a Sequence.

Husk, 10 9 bytes

Edit: -1 byte thanks to Razetime

►LfoεṠ-uQ

Try it online!

►           # output the (first) element with maximum...
 L          # ...length...
            # ...from among:
         Q  # all contiguous sublists, 
  fo        # filtered by:
      Ṡ-    # the difference between...
        u   # ...itself and unique elements of itself...
    ε       # ...has length 1.

05AB1E, 11 bytes

ŒʒgyÙg-!}éθ

Try it online or verify all test cases.

Explanation:

Œ        # Get all sublists of the (implicit) input-list
 ʒ       # Filter this list of sublists by:
  g      #  Get the length of the sublist
   y     #  Push the sublist again
    Ù    #  Uniquify its items
     g   #  Pop and push the length of this uniquified list as well
      -  #  Subtract the lengths from one another
       ! #  Check if this is either 0 or 1 by taking the faculty, which will
         #  result in [1,1,2,6,24,120,...] for [0,1,2,3,4,5,...] respectively,
         #  and only 1 is truthy in 05AB1E
 }é      # After the filter: sort the remaining sublists from shortest to longest
   θ     # Pop and keep just the last (longest)
         # (after which it is output implicitly as result)

Charcoal, 40 38 bytes

≔⟦⟧ηFθ«⊞υιW‹¹ΣEυ›μ⌕υλ≔Φυμυ¿›LυLη≔Iυη»η

Try it online! Link is to verbose version of code. Explanation:

≔⟦⟧η

Start with no longest sublist.

Fθ«

Loop over the input terms.

⊞υι

Append the term to the predefined empty list.

W‹¹ΣEυ›μ⌕υλ

While the current sublist contains more than one duplicate...

≔Φυμυ

... remove the first term from the sublist.

¿›LυLη

If the sublist is longer than the longest sublist so far, then...

≔Iυη

... save a copy of the sublist (because we're going to push to the original, and we need to cast it to string anyway).

»η

Output the longest sublist (which we already cast to string above).

Haskell, 80 bytes

import Data.List
l=length
head.sortOn(\x->(l(x\\nub x)>1,-l x)).(tails=<<).inits

Try it online!

Get all infixes, sort by (invalid, -length), return the first one.

Haskell, 108 83 bytes

f l@(u:q)|0:(0<$l)<[0|x<-l,y<-q,x==y]=f(init$l)!f q
f q=q
a!b|(a<$a)>(a<$b)=a|1>0=b

Try it online!

g : counts duplicates. -> moved to list comprehension in f by Wheat Wizard , the tricky part is here and deserves an explanation

f(u:q)|
(0<$q)       list 0s for each element 
<            compared to
[0|x<-q,y<-q,x==y] list of 0s for each equal pairs in the powerset

Note that we use tail so that one potential dupe is already counted and the lists must be the same, if not there is more than 1 dupe, in that case we search.. Else we skip this and f q=q match giving the result.

a!b : selects longer list.
flist : if list is not ok choose from head or tail else return it

Factor + math.unicode, 77 bytes

[ f suffix all-subseqs [ histogram values 1 v-n Σ 2 < ] find-last nip sift ]

Try it online!

Explanation

The quotation to find-last works by taking a histogram of the input, subtracting 1 from all the counts, taking the sum, and checking whether the sum is less than two. For instance:

             ! { 2 2 3 2 4 }
histogram    ! H{ { 2 3 } { 3 1 } { 4 1 } }
values       ! { 3 1 1 }
1 v-n        ! { 2 0 0 }
Σ            ! 2
2 <          ! f

Brachylog, 14 12 10 bytes

-2 bytes thanks to xash

⊇.ọtᵐ×≤2&s

Try it online!

Explanation

We're looking for a sublist in which each element occurs only once, except that one element may occur twice.

Makes use of this very helpful tip from Unrelated String.

⊇.ọtᵐ×≤2&s
            The input
⊇           Sublist (not necessarily contiguous), trying longest first
 .          The result is the output
  ọ         Generate a list of [element, count] pairs
   tᵐ       Get the last element of each pair (list of counts)
     ×      Product
      ≤2    Is less than or equal to 2
        &s  Also, the output is a contiguous sublist of the input

R, 162 bytes

Or R>=4.1, 148 bytes by replacing two function occurrences with \s.

function(v,a=apply(combn(rep(seq(v),2),2),2,function(x,t=table(k<-v[x[1]:x[2]]))if(x[1]<=x[2]&all(t%in%1:2)&sum(t==2)<2)k))"if"(sum(v),a[order(-lengths(a))[1]],v)

Try it online!

I was hoping for some ingenious answer from better R golfers here, but it looks like I have to post my boring straightforward one first for some motivation.

Burlesque, 28 bytes

su<-{f:)-]g_2<=j{1==}al&&}fe

Try it online!

su           # All substrings from shortest to longest
<-           # Reverse (longest first)
{
 f:          # Frequency list as (count, val) sorted by count
 )-]         # Map head (get counts)
 g_          # Split into head + tail
 2<=         # Head is 2 or 1
 j           # Swap
 {1==}al     # Tails all 1s
 &&          # Logical AND
}fe          # Find the first element for which

Ruby, 63 bytes

f=->l,s=1{l[-s]&&f[l,s+1]||l.each_cons(s).find{|q|q.uniq[s-2]}}

Try it online!

Jelly, 11 bytes

ẆµĠẈ’SỊµƇṪȯ

Try It Online!

-1 byte thanks to Jonathan Allan

ẆµĠẈ’SỊµƇṪȯ   Main Link
Ẇ             all sublists
 µ     µƇ     filter
  Ġ           - group indices of equal elements
   Ẉ          - length of each group
    ’         - subtract one from each length
     S        - sum of these (total number of duplicate elements)
      Ị       - is this insigificant? (between -1 and 1)
         Ṫ    take the last (longest) element
          ȯ   logical OR; if the output is 0 (only happens if input
              is []), return the input instead, which fixes [] being
              output as 0

JavaScript (Node.js), 64 bytes

f=(a,...r)=>a[new Set(a).size+1]?f(...r,a.slice(1),a.pop()&&a):a

Try it online!


Python 3, 58 bytes

f=lambda l,*r:len(l)>1+len({*l})and f(*r,l[1:],l[:-1])or l

Try it online!

Yet another question solved with same pattern?

Jelly, 10 bytes

Ẇœ-QḊƊÐḟṪȯ

Try it online!

How?

Ẇœ-QḊƊÐḟṪȯ - Link: list, A
Ẇ          - substrings (well, non-empty ones) sorted by length then left to right
      Ðḟ   - filter discard those for which this is truthy:
     Ɗ     -   last three links as a monad:
   Q       -     distinct values
 œ-        -     multiset difference - remove one of each value from A
    Ḋ      -     dequeue - remove one from this list if there are any
                         - [] is falsey
        Ṫ  - tail - one of, or the, longest one
                  - unless A was [] in which case zero
         ȯ - logical OR with A - replace that zero with []

Alternatively, Ẇœ-QLỊƲƇṪȯ.

Retina 0.8.2, 78 bytes

M!&r`.*\d\b
.*\b(\d+)(?=.*,\1\b),(.*\b(\d+)\b.*,\3\b)
$2
O$^`((,)|\d)+
$#2
1G`

Try it online! Link includes test cases. Explanation:

M!&r`.*\d\b

Take all of the suffixes.

.*\b(\d+)(?=.*,\1\b),(.*\b(\d+)\b.*,\3\b)
$2

Truncate them at the second last duplicate. (Since we need this longish regex to check for two pairs/three of a kind, it turns out to be golfier to truncate the prefixes than to filter on all sublists.)

O$^`((,)|\d)+
$#2

Sort by descending number of elements.

1G`

Take the first.

JavaScript (Node.js), 79 75 bytes

f=(X,a)=>X[h=new Set([w,...x]=X).size+1]?(a=f(x),b=f(X,X.pop()),a[h]?a:b):X

Try it online!

AnttiP's idea, can't read golfing language so don't know if that's source

JavaScript (Node.js), 89 bytes

f=(x,h,L=0,a=x.filter(y=x=>!(y[x]=h+=[y[x]])[20]&&++L),b=x[0]?f(x.slice(1)):[])=>b[L]?b:a

Try it online!

Is it usual to be 6-8x longer than golfing language or this is quite golfable?

Python 3, 68 bytes

f=lambda l:len(l)>1+len({*l})and max(f(l[1:]),f(l[:-1]),key=len)or l

Try it online!

Japt -h, 14 bytes

Feel like I'm missing a trick here :\

ã fÈÊɶXâ ÊÃiU

Try it