Challenge:
Given a positive integer, output the longest single-digit subsequence that occurs at least twice, AND has boundaries of another digit (or the start/end of the integer).
An example:
Input: 7888885466662716666
The longest subsequence of a single digit would be 88888 (7[88888]5466662716666) with a length of 5. However, this subsequence only occurs once in the integer.
Instead, the result for input 7888885466662716666 should be 6666 (78888854[6666]271[6666]), since it occurs (at least) twice.
Challenge rules:
- Length of the subsequences takes priority over the amount of times it occurs. (I.e. with input
8888858888866656665666, we output88888([88888]5[88888]66656665666; length 5, occurs twice), and not666(88888588888[666]5[666]5[666]; length 3, occurs thrice). - If the length of multiple subsequences are equal, we output the one with the largest occurrence-count. I.e. with input
3331113331119111, we output111(333[111]333[111]9[111]; length 3, occurs thrice), and not333([333]111[333]1119111; length 3 as well, but occurs twice) - If the occurrence-count and length of multiple subsequences are equal, you can output either of them, or all (in any order). I.e. with input
777333777333, the possible outputs are:777;333;[777, 333]; or[333, 777]. - The subsequence must have boundaries of other digits (or the start/end of the integer). I.e. with input
122222233433the result is33(1222222[33]4[33]; length 2, occurs twice) and not222(1[222][222]33433, length 3, occurs twice with both invalid).- This applies to all numbers that are counted towards the occurrence-counter. I.e. with input
811774177781382the result is8([8]117741777[8]13[8]2; length 1, occurs thrice) and not77(811[77]41[77]781382/811[77]417[77]81382; length 2, occurs twice with one invalid) nor1(8[1][1]774[1]7778[1]382; length 1, occurs four times with two invalid).
- This applies to all numbers that are counted towards the occurrence-counter. I.e. with input
- You can assume the input won't contain any digits
0(it will match[1-9]+). (This is to avoid having to deal with test cases like10002000that should output000, where most languages would output0by default.) - You can assume the input will always contain at least one valid output.
- I/O are both flexible. Can be a list/array/stream of digits/bytes/characters or as string instead of a single integer.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input: 7888885466662716666 / [7,8,8,8,8,8,5,4,6,6,6,6,2,7,1,6,6,6,6]
Output: 6666 / [6,6,6,6]
Input: 3331113331119111 / [3,3,3,1,1,1,3,3,3,1,1,1,9,1,1,1]
Output: 111 / [1,1,1]
Input: 777333777333 / [7,7,7,3,3,3,7,7,7,3,3,3]
Possible outputs: 777; 333; [777,333]; [333;777] / [7,7,7]; [3,3,3]; [[7,7,7],[3,3,3]]; [[3,3,3],[7,7,7]]
Input: 122222233433 / [1,2,2,2,2,2,2,3,3,4,3,3]
Output: 33 / [3,3]
Input: 811774177781382 / [8,1,1,7,7,4,1,7,7,7,8,1,3,8,2]
Output: 8 / [8]
Input: 555153333551 / [5,5,5,1,5,3,3,3,3,5,5,1]
Output: 1 / [1]
Input: 12321 / [1,2,3,2,1]
Possible outputs: 1; 2; [1,2]; [2,1] / [1]; [2]; [[1],[2]]; [[2],[1]]
Input: 944949949494999494 / [9,4,4,9,4,9,9,4,9,4,9,4,9,9,9,4,9,4]
Output: 4 / [4]
Input: 8888858888866656665666 / [8,8,8,8,8,5,8,8,8,8,8,6,6,6,5,6,6,6,5,6,6,6]
Output: 88888 / [8,8,8,8,8]
Input: 1112221112221111 / [1,1,1,2,2,2,1,1,1,2,2,2,1,1,1,1]
Output: 111; 222; [111,222]; [222,111] / [1,1,1]; [2,2,2]; [[1,1,1],[2,2,2]]; [[2,2,2],[1,1,1]]
Input: 911133111339339339339339 / [9,1,1,1,3,3,1,1,1,3,3,9,3,3,9,3,3,9,3,3,9,3,3,9]
Output: 111 / [1,1,1]
8888858888866656665666. If I interpreted the challenge correctly, both the Brachylog and 05AB1E solutions fail. \$\endgroup\$222when bounded by other integers. I guess we just shouldn't count the occurrence that is a substring of1111. Better wait for the OP though, indeed. \$\endgroup\$1112221112221111these are the subsequences and their counts:1111 (1),111 (2),222 (2). Since we only outputs sequences occurring at least twice, the output can be one of:111,222,[111,222],[222,111]. (See the fourth rule for some more information.) Basically1111will only ever count as1111, and not as1and111or11and11. I'll add your test case, but the output is either or both of111and222. \$\endgroup\$