| Bytes | Lang | Time | Link |
|---|---|---|---|
| 006 | Husk | 250209T141224Z | Glory2Uk |
| 085 | Vyxal o | 230927T191339Z | pacman25 |
| 056 | Perl 6 | 180917T104721Z | nwellnho |
| 085 | R | 180919T071852Z | digEmAll |
| 152 | PCRE | 180919T073229Z | jaytea |
| 012 | Japt h | 180918T224804Z | Shaggy |
| 067 | Wolfram Language Mathematica | 180917T214350Z | LegionMa |
| 072 | Haskell | 180918T203348Z | nimi |
| 102 | R | 180918T181034Z | Kirill L |
| 067 | Ruby | 180918T081106Z | Kirill L |
| 213 | Java JDK 10 | 180918T104144Z | Olivier |
| 250 | Red | 180918T091521Z | Galen Iv |
| 088 | Perl 5 | 180917T200440Z | Kjetil S |
| 068 | JavaScript ES6 | 180917T092714Z | Arnauld |
| 113 | Python 2 | 180917T133818Z | ovs |
| 014 | 05AB1E | 180917T081440Z | Emigna |
| 101 | Powershell | 180917T132046Z | mazzy |
| 012 | Jelly | 180917T081007Z | Mr. Xcod |
| 120 | Python 2 | 180917T075921Z | TFeld |
| 056 | Retina | 180917T080029Z | Neil |
Husk, 13 6 bytes
►LṠ-ug
Edit
Turned out, this task could be solved with a very short piece of code.
Explanation
It has taken a while to figure out the correct solution. It boils down to those essential steps:
- Removal of the items appearing one time only. Here, I was able to improve from these 9-byte-monsters
ΣtġLÖLgOgorΣfo>1LgOg(filtering out the list elements) to the more elegantṠ-ug(subtraction of every unique element). See also the Jelly answer below. - Get the most frequent element from the elements of the maximal length.
Commented:
g -- group adjacent numbers into sublists
u -- get unique groups / subsequences
Ṡ- -- remove unique subsequences from the whole list:
-- | this will allow to get rid of the subsequences appearing only once
►L -- take the most common item from the elements of the maximal length.
Old version:
g -- group adjacent numbers into sublists
u -- get unique groups / subsequences
Ṡ- -- remove unique subsequences from the whole list:
-- | this will allow to get rid of the subsequences appearing only once
ÖL -- sort by length
ġL -- group subsequences by length
→ -- keep the longest subsequence(s)
►Lg -- take the most common item
u -- make unique
Vyxal o, 68 bitsv2, 8.5 bytes
Ġ:UÞ⊍⁽LO∆M
groups by identical adjacent digits, finds the duplicated elements, then takes the most common of the longest runs.
R, 85 bytes
function(x,R=rle(x),a=ave(R$v,R,FUN=length))rep(R$v[o<-order(a<2,-R$l,-a)[1]],R$l[o])
Input : a vector of separated integer digits e.g.
c(1,8,8...)Output : a vector of separated integer digits
Unrolled code with explanation :
function(x){ # x is a vector of digits : e.g. c(1,1,8,8,1,1)
R = rle(x) # Get the sequences of consecutive repeating digits
# doing run length encoding on x, i.e. : R is a list
# with the digits (R$values) and the number of their
# consecutive occurrencies (R$lengths)
# N.B. you can use R$v for R$values and R$l for R$lenghts
a=ave(R$v,R,FUN=length) # Group R$v by R$l AND R$v, count the occurrencies
# for each group and "unroll" the value of each
# group to the original R$v length.
# Here basically we count the occurrencies of the same
# sequence.
o<-order(a<2,-R$l,-a)[1] # Get the indexes used to order by a < 2 then by -R$l and
# finally by -a; store the first index in "o".
# Here basically we use order to select the first sequence
# repeated at least twice, in case of ties the sequence
# with the greatest length and in case of ties the most
# repeated sequence.
rep(R$v[o],R$v[o]) # Using the index "o", we reconstruct the sequence repeating
# R$l[o] times R$v[o]
}
Alternative version accepting vector of integer or character digits :
R, 88 bytes
function(x,R=rle(x),a=ave(R$v,R,FUN=length))rep(R$v[o<-tail(order(a>1,R$l,a),1)],R$l[o])
Input : a vector of separated characters or digits e.g.
c("1","8","8"...)orc(1,8,8...)Output : a vector of separated characters if the input was a vector of characters, a vector of digits if the input was a vector of digits
PCRE, 152 bytes
(\d)(?<!(?=\1)..)(?=(\1*)(?!\1).*(?!\1).\1\2(?!\1))(?!(?:(?=\2((\3?+)(\d)(\5*)))){1,592}?(?=\2\3.*(?!\5).\5\6(?!\5))(?:\1(?=\1*\4\5(\7?+\5)))*+(?!\1))\2
See it in action on: https://regex101.com/r/0U0dEp/1 (just look at the first match in each test case)
This is just for fun, since regex isn't a real programming language in and of itself, and the solution is limited :P
Because a zero-width group such as (?:)+ only matches once and doesn't repeat indefinitely, and because PCRE internally makes copies of groups quantified with limits, I've had to use a magic number in there ("{1,592}"), which means we can only look up to 592 contiguous sets of digits ahead to find a competing set that could be longer than the one currently under inspection. More info on this concept here.
Wolfram Language (Mathematica), 67 bytes
#&@@@MaximalBy[Select[Tally@Split@#,Last@#>1&],{Length@#,#2}&@@#&]&
Pure function. Takes a list of digits as input and returns a list of subsequences (in no particular order) as output. Not sure if the "must appear at least twice" clause can be handled more cleanly. Try it online!
Haskell, 72 bytes
import Data.Lists
g!x|y<-countElem x g=(y>1,1<$x,y)
(argmax=<<(!)).group
How it works
(argmax=<<(!)).group -- expands to: f i = argmax (group i !) (group i)
group -- split the input list into subsequences of equal digits
-- e.g. "1112211" -> ["111","22","11"]
-- find the element of this list where the function !
-- returns the maximum value. First parameter to !
-- is the grouped input list, second parameter the
-- the element to look at
g!x|
y<-countElem x g -- let y be the number of occurrences of x in g
= ( , , ) -- return a triple of
y>1 -- a boolean y>1 (remember: True > False)
1<$x -- length of x (to be exact: all elements in x
-- replaced by 1. This sorts the same way as the
-- length of x)
y -- y
-- a triples sorts lexicographical
R, 102 bytes
function(i)rep(names(sort(-(x=(x=table(rle(i)))[rowSums(x>1)>0,,drop=F])[m<-max(rownames(x)),])[1]),m)
Since there wasn't an R answer yet, I decided to give it a try, and well... it wasn't easy. I don't really know whether it is a good approach, but here it goes.
Inputs and outputs vectors of characters.
Ruby, 68 67 bytes
->a{(b=a.chunk &:+@).max_by{|x|[(c=b.count x)<2?0:x[1].size,c]}[1]}
Inputs and outputs arrays of chars.
The approach is pretty straightforward: we identify the runs of consecutive digits (chunk using unary + as identity function) and take the maximum - first by the size of the run (reset to zero if its occurrence count is < 2), then by the count itself.
Java (JDK 10), 213 bytes
s->{int l=99,X[][]=new int[10][l],d,D=0,m=0,M=0;for(var x:s.split("(?<=(.))(?!\\1)"))X[x.charAt(0)-48][x.length()]++;for(;M<1&&l-->1;)for(d=0;d++<9;)if((m=X[d][l])>1&m>M){M=m;D=d;}for(;l-->0;)System.out.print(D);}
Explanation (outdated)
s->{ // Lambda for Consumer<String>
int l=99, // Length of token, max is 99.
X[][]=new int[10][l], // Array containing the occurrences per token
d, // digit value
D=0, // digit holder for best sequence candidate
m=0, // holder of the current candidate
M=0; // best candidate for the current length of token.
for(var x:s.split("(?<=(.))(?!\\1)")) // Tokenize the string into digit-repeating sequences
X[x.charAt(0)-48][x.length()]++; // Add one occurrence for the token
for(;M<1&&l-->1;) // While no value has been found and for each length, descending. Do not decrease length if a value has been found.
for(d=0;d++<9;) // for each digit
if((m=X[d][l])>1&m>M){ // if the current occurrence count is at least 2 and that count is the current greatest for the length
M=m;D=d; // mark it as the current best
} //
for(;l-->0;)System.out.print(D); // Output the best-fitting subsequence.
} //
Credits
- -9 bytes and bug spotting thanks to Kevin Cruijssen
Red, 256 250 bytes
func[s][p: func[b][sort parse b[collect[any keep[copy a skip thru any a]]]]first
last sort/compare collect[foreach d p p s[if 1 < k: length? to-block d[keep/only
reduce[form unique d k]]]]func[x y][(reduce[length? x/1 x/2])< reduce[length? y/1 y/2]]]
Really, realy long solution this time... (sigh)
Takes the input as a string.
Explanation:
f: func [ s ] [
p: func [ b ] [ ; groups and sorts the adjacent repeating items
sort parse b [
collect [
any keep[
copy a skip thru any a ; gather any item, optionally followed by itself
]
]
]
]
t: copy []
foreach d p p s [ ; p p s transforms the input string into a block of sorted blocks of repeating digits
if 1 < k: length? to-block d [ ; filters only the blocks that occur more than once
insert/only t reduce [ form unique d k ] ; stores the digits and the number of occurences
; "8888858888866656665666" -> [["5" 3] ["666" 3] ["88888" 2]]
]
]
first last sort/compare t func [ x y ] ; takes the first element (the digits) of the last block of the sorted block of items
[ (reduce [ length? x/1 x/2 ]) < reduce [ length? y/1 y/2 ] ] ; direct comparison of the blocks
]
Perl 5, 88 bytes
my($m,%s);++$i%2*$s{$_}++&&($n=$s{$_}/9+length)>$m&&($a=$_,$m=$n)for pop=~/((.)\2*)/g;$a
Slightly ungolfed, with tests:
sub f {
my($m,%s);
my($i,$n,$a); #not needed in golfed version
++$i % 2 * $s{$_}++
&& ($n=$s{$_}/9+length) > $m
&& ($a=$_, $m=$n)
for pop=~/((.)\2*)/g; #i.e. 7888885466662716666 => 7 88888 5 4 6666 2 7 1 6666
$a
}
for(map[/\d+/g],split/\n/,join"",<DATA>){ #tests
my($i,@e)=@$_;
printf "%-6s input %-24s expected %-10s got %s\n",
(grep f($i) eq $_, @e) ? "Ok" : "Not ok", $i, join('|',@e), f($i);
}
__DATA__
Input: 7888885466662716666 Output: 6666
Input: 3331113331119111 Output: 111
Input: 777333777333 Output: 777|333
Input: 122222233433 Output: 33
Input: 811774177781382 Output: 8
Input: 555153333551 Output: 1
Input: 12321 Output: 1|2
Input: 944949949494999494 Output: 4
Input: 8888858888866656665666 Output: 88888
Input: 1112221112221111 Output: 111|222
JavaScript (ES6), 79 73 68 bytes
Takes input as a string. Returns an integer.
s=>[...s,r=q=0].map(o=d=>q=s^d?o[!o[q]|r[q.length]?q:r=q]=s=d:q+d)|r
Commented
s => // s = input string, also used as the current digit
[ ...s, // split s into a list of digit characters
r = // r is the final result
q = // q is the current digit sequence
0 // append a final dummy entry to force the processing of the last
] // sequence
.map(o = // o is an object used to keep track of encountered sequences
d => // for each digit d in the array defined above:
q = // update q:
s ^ d ? // if d is not equal to the current digit:
o[ // this statement will ultimately update o[q]
!o[q] | // if q has not been previously seen
r[q.length] ? // or the best result is longer than q:
q // leave r unchanged
: // else:
r = q // set r to q
] = s = d // reset q to d, set the current digit to d
// and mark q as encountered by setting o[q]
: // else:
q + d // append d to q
) | r // end of map(); return r, coerced to an integer
Python 2, 114 113 bytes
-1 byte thanks to TFeld.
p='';r=[];C=r.count
for c in input():r+=['']*(c!=p);r[-1]+=c;p=c
print max((len(w),C(w),w)for w in r if~-C(w))[2]
05AB1E, 14 bytes
γТ1›ÏD€gZQÏ.M
Explanation
γ # group consecutive equal elements
Т # count the occurrence of each group among the list of groups
1›Ï # keep only groups with a count greater than 1
D€gZQÏ # keep only those with a length equal to the greatest length
.M # get the most common item
Powershell, 101 byte
($args|sls '(.)\1*'-a|%{$_.Matches}|group|?{$_.Count-1}|sort @{e={$_.Name.Length,$_.Count}})[-1].Name
Explanied test script:
$f = {
(
$args| # for each argument (stings)
sls '(.)\1*'-a| # searches all
%{$_.Matches}| # regex matches
group| # group it (Note: Count of each group > 0 by design)
?{$_.Count-1}| # passthru groups with Count not equal 1
sort @{ # sort all groups by 2 values
e={$_.Name.Length,$_.Count}
}
)[-1].Name # returns name of last group (group with max values)
}
@(
,('7888885466662716666', '6666')
,('3331113331119111', '111')
,('777333777333', '777','333')
,('122222233433', '33')
,('811774177781382', '8')
,('555153333551','1')
,('12321', '1','2')
,('944949949494999494','4')
,('8888858888866656665666','88888')
,('1112221112221111','111','222')
) | % {
$s,$e = $_
$r = &$f $s
"$($r-in$e): $r"
}
Output:
True: 6666
True: 111
True: 777
True: 33
True: 8
True: 1
True: 1
True: 4
True: 88888
True: 111
Jelly, 12 bytes
Œgœ-Q$LÐṀÆṃ'
Previous version – 14 bytes
ŒgŒQ¬TịƲLÐṀÆṃ'
How it works?
Œgœ-Q$LÐṀÆṃ' – Full program. Receives a list of digits as input.
Œg – Group equal adjacent values.
œ-Q$ – Multiset difference with itself deduplicate.
LÐṀ – Keep those that are maximal by length.
Æṃ' – Mode. Returns the most common element(s).
-------------------------------------------------------------------------
ŒgŒQ¬TịƲLÐṀÆṃ' – Full program. Receives a list of digits as input.
Œg – Group equal adjacent values.
ŒQ – Distinct sieve. Replace the first occurrences of each value by 1.
and the rest by 0. [1,2,3,2,3,2,5]ŒQ -> [1,1,1,0,0,0,1]
¬T – Negate and find the truthy indices.
ịƲ – Then index in the initial list of groups.
– This discards the groups that only occur once.
LÐṀ – Find all those which are maximal by length.
Æṃ' – And take the mode.
Python 2, 123 120 bytes
import re
def f(s):r=re.findall(r'(\d)(\1*)',s);c=r.count;print max((len(a+b)*(c((a,b))>1),c((a,b)),a+b)for a,b in r)[2]
Retina, 56 bytes
L`(.)\1*
O`
L$m`^(.+)(¶\1)+$
$#2;$1
N`
.+;
N$`
$.&
-1G`
Try it online! Link includes test cases. Explanation:
L`(.)\1*
List all the maximally repeated digit subsequences.
O`
Sort the list into order.
L$m`^(.+)(¶\1)+$
$#2;$1
List all the multiple subsequences with their "count".
N`
Sort in ascending order of count.
.+;
Delete the counts.
N$`
$.&
Sort in ascending order of length. (Where lengths are equal, the previous order due to count is preserved.)
-1G`
Keep the last i.e. longest value.