| Bytes | Lang | Time | Link |
|---|---|---|---|
| 045 | Wolfram Language Mathematica | 220130T031059Z | att |
| 009 | Husk | 220130T093308Z | Dominic |
| 011 | 05AB1E | 220131T091509Z | Kevin Cr |
| 038 | Charcoal | 220128T195932Z | Neil |
| 080 | Haskell | 220130T102101Z | ovs |
| 083 | Haskell | 220130T002019Z | AZTECCO |
| 077 | Factor + math.unicode | 220130T045540Z | chunes |
| 010 | Brachylog | 220128T173450Z | DLosc |
| 162 | R | 220129T201349Z | pajonk |
| 028 | Burlesque | 220129T114522Z | DeathInc |
| 063 | Ruby | 220129T083929Z | G B |
| 011 | Jelly | 220128T173112Z | hyperneu |
| 064 | JavaScript Node.js | 220129T013033Z | tsh |
| 010 | Jelly | 220128T223002Z | Jonathan |
| 078 | Retina 0.8.2 | 220128T185223Z | Neil |
| 075 | JavaScript Node.js | 220128T182020Z | l4m2 |
| 068 | Python 3 | 220128T180419Z | AnttiP |
| 014 | Japt h | 220128T173359Z | Shaggy |
Wolfram Language (Mathematica), 48 47 45 bytes
Last@*Select[1##&@@Counts@#<3&]@*Subsequences
Input [list]. Output a list.
Wolfram Language (Mathematica), 45 bytes
f[s=___,Longest@a:s,s]/;Times@@Counts@{a}<3=a
Input [list...]. Output a Sequence.
Husk, 10 9 bytes
Edit: -1 byte thanks to Razetime
►LfoεṠ-uQ
► # 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
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
- Thanks to @Wheat Wizard for saving so many many bytes!
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 ]
Explanation
f suffix ... siftWork around the fact thatall-subseqsblows up on an empty input. Addfto the input and remove it withsiftat the end. (This doesn't change anything since we're only adding one.)all-subseqs [ ... ] find-last nipFind the last/largest subsequence of the input that returns true when the given quotation is called on it.
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
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)
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
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
Jelly, 11 bytes
ẆµĠẈ’SỊµƇṪȯ
-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
Python 3, 58 bytes
f=lambda l,*r:len(l)>1+len({*l})and f(*r,l[1:],l[:-1])or l
Yet another question solved with same pattern?
Jelly, 10 bytes
Ẇœ-QḊƊÐḟṪȯ
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
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
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