[Question]
  [
The purpose of this task is to write a program or function to find the most common substring within a given string, of the specified length.
## Inputs
1. A string, `s`, of any length and characters that are valid as inputs for your language.
2. A number, `n`, indicating the length of substring to find. Guaranteed to be equal to or less than the length of the string.
## Output
The most common (case-sensitive) substring of `s` of length `n`. In the case of a tie, any of the options may be output.
## Win Criteria
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so lowest bytes wins; usual exceptions etc. apply.
## Examples
`Hello, World!`, `1` > `l` (appears 3 times)  
`Hello! Cheerio!`, `2` > `o!` (appears twice)  
`The Cat sat on the mat`, `2` > `at` (appears three times)  
`The Cat sat on the mat`, `3` > `at`  or `he`  (both with trailing spaces. Both appear twice - note case-sensitivity so `The` and `the` are different substrings)  
`Mississippi`, `4` > `issi` (appears twice, note that the two occurrences overlap each other)
## Some more examples, all using the same input string\*
```
Bb:maj/2
F:maj/5
Bb:maj/2
G:9/3
Bb:maj7
F:maj/3
G:min7
C:sus4(b7,9)
C:sus4(b7,9)
C:sus4(b7,9)
F:maj
F:maj
```
(note the trailing newline)
1 > `\r\n`(appears 12 times, counting `\r\n` as a single character - my choice, your code may differ on this - otherwise either `\r` or `\n` appear the same number of times). `:` also appears 12 times  
2 > `:m` (appears 8 times)  
3 > `:ma` or `maj` (appears 7 times)  
4 > `:maj` (appears 7 times)  
5 > `F:maj` or `\r\nF:ma` or `:maj/` (appears 4 times)  
14 > `\r\nC:sus4(b7,9)\r\n` (appears 3 times)
(\* input is taken from [How predictable is popular music?](https://codegolf.stackexchange.com/questions/198425/how-predictable-is-popular-music/). The result of this task might be used, for example, to find the most efficient way to apply a substitution in order to compress the string)
  
[Answer]
# Python 3.8, ~~[75](https://tio.run/##Jc7BasMwEATQX5nmEok4hrSXEsgH9N5bmoMsry1RZWW0a7v@ele0p4VhHrPTpiHz2/tU9gG3rz25Z9c7SMNXyUWpN8ZI6/PMapbrTe7xGk/8sM1ih1wQERnF8UgmERuxZz5drLX38@WxTyVWNZjDZ4gCCTmmHh1B8pOq84UkdomQMo@YInlCHqD0o7UCDU6hI9VAFN4JVesrFRRyKW31JFocawPHPQL57wZRQQsxgpP/Hd2mLPhAH3s@KrqsYS3wuRTyGnls2xbdrMgBK6XUIJEeqyXC@vdBoDokc1JEeTk0eLV2/wU)~~ ~~[68](https://tio.run/##Jc/BTsMwDAbgV/nZZalWKgEXNGkPsDs34JCmbmOROlXsduvTjwhOln75028vu8Usb@9LeYy4fD2Sn/vBQ1s5z/7unNMu5FXMbeeLfvKZT/LdtFsz5gIGC4qXiVwicdo8y@mlaZrHUriK0R0@Iis0Zk4DeoLmmaoJhZT7REhZJixMgZBHGN2trsCiN9hENVBD8ErVhkoVhXxKex2JNi/WwsuASOGnBRtoI0H0@t9j@5IVVww8yNHQZ4u3gpBLoWAsU9d16FdDjrhRSi0S2bFaItz@LohUi3RNBtanQ4vX@tkv)~~ ~~[97](https://tio.run/##XcoxDsIwDEDRq1idbBFYYECRepLQIUADjlonitsKTh/SFelPXy9/l3eS8zWXGqC/1cnP96cHNWJn/0FEpy7aeJAhpAIRWKB4eY04jYJKNJweaZUFN9urY8sNktlox/yPj0JENRduPmDHqnudgUvbPw)~~ 63 bytes
```
lambda s,n:max(l:=[s[j:j+n]for j in range(len(s))],key=l.count)
```
You can [try it online](https://tio.run/##FcJBCsMgEADAryw57RLppT0UwZekOdhUW41ZxTWQvN7QYcrZfpnvz1K7B/PqyW7vjwVRrDd7YNJmkinqOPLsc4UIgaFa/jpMjlGIZrW606Tbkndu1EsN3NDjEET@BwUPon4B)! Increased byte count because, as @xnor kindly pointed out, Python's `str.count` doesn't count overlapping substrings... But then @xnor's reformulation of what I was doing allowed to slice a third of the bytes!
## How:
```
l:=[s[j:j+n]for j in range(len(s))]
```
This creates a list of all the substrings in `s` of size `n`, plus the suffixes of size `n-1`, `n-2`, ..., `1`. Then we find the `max` on that list, with the numerical value being used to sort given by
```
l.count
```
That means that if `a` and `b` are from `l`, `a > b` if `a` shows up more times in `l` than `b`. This means the shorter suffixes are never the result of the `max` because they only show up once, and even if the correct-sized substrings only show up once, they are first in `l` so they come out instead of the short suffixes. This allows me to save some bytes in the `range` used, given that I don't have to prevent `i+n` from being larger than `len(s)`.
~~# Python, ~~[83](https://tio.run/##LY89bsMwDIWv8uolFuIYSLsF6AG6d0szyDJtEVEoQ2SS@vSu0HYiSPB7P8tqMcvbNuH9a0v@Nowe2slJczEa21bPfOK9XKZcwGBB8TJTm0hadQfZH53rrrS@/6N20j7ku1hrzp0Px8u2FK5bs2uwx9Q2n5EVGjOnEQNB842qaiikPCRCyjJjYQqEPMHo2@oLLHqDzVQPagheqbKhoopCPqW1jkQPL9bBy4hI4dqBDfQgQfT652PrkhUfGHmUnWHIFp8FIZdCwVjmvu8x3A054kkpdUhku8oS4fmbIFI10nsysL40HV5dLVWrue0H)~~ 64 bytes~~
```
lambda s,n:max((s[i:i+n]for i in range(len(s)-n+1)),key=s.count)
```
Thanks to @mypetlion I saved a LOT of bytes :D
You can [try it online](https://tio.run/##NY/BbsMwDEN/hculCZoF2HYrsL/YbdvBsZnaqGMHltI2X5@5KHYRIIGk@JZNfU4f@4TPnz2aeXQG0qfTbO5tK9/hFI7pd8oFASGhmHRmG5la6V7T8a3r@gu3TxlsXpN2@1JC0rY5NDhiapsvHwTi8xodRkLyzJpiC10Y44aY0xlLoCXyBOVdqwTqjdbBehCFNcLqtdUqKDSx@gojryZpD5McPO2lR1DwygRv3POPbkuWZ9hjH7PbwFjDbC6FVunwwJo5DAPGVZE9boyxR6Qeam0St/8qhbJGRZCXpsd7V@kqY7f/AQ) with my very own incredible test case!
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 5 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ŒIù.M
```
[Try it online](https://tio.run/##yy9OTMpM/f//6CTPwzv1fP//D8lIVXBOLFEoBuL8PIUSIDc3sYTLCAA) or [verify the smaller test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWXC/6OTig/v1PP9r/M/OtpQR8kjNScnX0chPL8oJ0VRKVZHIdoIKqio4JyRmlqUmQ8XDslIVXBOLFEoBuL8PIUSIDc3sQQsa4xX1kRHyTezuBiECgoylWJjAQ) or [verify the larger test case](https://tio.run/##yy9OTMpM/X90kmdZpb2SwqO2SQpK9i6V/w/v1PP9r/NfSUnJKckqNzFL34jLDUybcsEF3K0s9Y2hXHOotDFQNDczz5zL2aq4tNhEI8lcx1ITDwesC0IC7eKKNtQx0jHWMdEx1TE0iQUA) (which is a bit slow).
**Explanation:**
```
Œ      # Push all substrings of the (implicit) input-string
 Iù    # Only keep substrings of a length equal to the second input-integer
   .M  # Only keep the most frequent item of the remaining substrings
       # (after which it is output implicitly as result)
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 8 bytes
```
s₎ᶠọtᵒth
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/v/hRU9/DbQse7u4tebh1UknG///RSr6ZxcUgVFCQqaSjYBL7PwoA "Brachylog – Try It Online")
### Explanation
```
  ᶠ        Find all…
s          …substrings of <1st element of input>…
 ₎         …of length <2nd element of input>
   ọ       Get the list of each substring with its number of occurrence
    tᵒ     Order by the number of occurrence
      t    Take the last one
       h   Output the substring itself
```
[Answer]
# JavaScript (ES6), ~~85 83~~ 80 bytes
Takes input as `(string)(length)`.
```
s=>n=>[...s].map(o=m=(x,i)=>(o[k=s.substr(i,n)]=-~o[k])<m|!k[n-1]?0:m=o[O=k])&&O
```
[Try it online!](https://tio.run/##jZFfa8IwFMXf@ymuPtgEYrraitgtHUz2h8HmwwZ7qAWjthptk9LUscHYV@9SHSpsD14SOPfc/EIOWfN3ruelKKquVIukTlmtWShZGFFKdUxzXiDFcoY@iMAsRCraME31dqarEgkiccy638aM8VX@1dpEsuvG1xdBzlQ0ZsbtdMb1XEmtsoRmaolS1H5IskwReFNltmi1MXIxhkM5DtiZbf2HtGC0SpJSqAbqHaEGUa0/zOsqgRGvQJutJFSmzXn1izYMr85nvAMDNqgSbDP5Qz8JrZtVFMIg/mms/SuboW1ZGhhMb2ZBztdOb1JO5N1O9ht5at8HQ8c7moPjUW8/zoXcmaNAb7WPZgMyxOf0u0sOYnppRS6BHgGPgE@gT8D1Y5qq8pbPV0gCC@E06ePL@Jma7xdyKdJPE1xjJLGp@gc "JavaScript (Node.js) – Try It Online")
[Answer]
# Java 10, ~~212~~ ~~211~~ ~~196~~ ~~195~~ ~~146~~ 143 bytes
```
s->n->{String r="",t;for(int m=0,c,j,i=s.length();i-->n;)for(c=0,j=-1;(j=s.indexOf(t=s.substring(i-n,i),j+1))>=0;)if(++c>m){m=c;r=t;}return r;}
```
-15 bytes by outputting the substrings with their count, [which is allowed](https://codegolf.stackexchange.com/questions/199696/the-most-common-substring/199718#comment474884_199696).  
-1 byte thanks to *@ceilingcat*.  
-52 bytes thanks to *@OlivierGrégoire*.
[Try it online.](https://tio.run/##jVNNb6MwEL3nV0x9soWhIUlVNSwcNlJ391D10JX20PTgEJOYhQHZprtVxG/PGgKKqm2rSnwM85793oyHXDwLP9/@PqaFMAbuhMLDBMBYYVUKuUODxqoiyBpMraowuB2CLw9WK9zxDzk/0Mqd1BxO5CSBDGI4Gj9BPzmckqBjQriNskpThRbKeMpTnnMVm6CQuLN7yiLluyUR6zipw/PYDyOaO4bCrfx7n1HrYtNsTL8lVT5yxXjuhYwl8TRiKqOelyYlO5RxGunYRq2WttEIOmqP0cSVXDebwpU8VP5cqS2Urhv05PLxCQTrOgNgpbE05OS7LIqKw69KF9sLwqIzOBvAC1jtpdSq@g/@uZewEhaMuysE6z5LYV@x5p9iLTi5U8Z0V12rEXp4MVaWQdXYoHbubYGU@CTQspbCeZ9OGetrhuFg@r3cyZCvm2Up8svZGm/74GqN59S35c3lfExcj5R5B5QKXWK1NI1Z0M01v2Eff/VLhxd513PnqjNdiFRSskbCydo92VDlODFqifIPuOjx6RDyGZ/zBb/i4aJlPe3tdmBMPOWRJRAvC0RdFy@9HBtidRJpJ@efoR@JvumdKI5DDWaYizdUjEdA4BY6MXwtZkYlHJXa4z8)
**Explanation:**
```
s->n->{         // Method with String and Integer parameters and String return-type
  String r="",  //  Result-String, starting empty
         t;     //  Temp-String
  for(int m=0,  //  Largest count, starting at 0
          c,    //  Temp count integer
          j,    //  Temp index integer
          i     //  Index-integer `i`
      =s.length();i-->n;)
                //  Loop `i` in the range (input-length, n]:
    for(c=0,    //   Reset the temp-count to 0
        j=-1;   //   And the temp-index integer to -1
        (j=s.indexOf(t=s.substring(i-n,i)
                //   Set String `t` to a substring of the input-String in the range [i-n,i)
           j+1))//   Set the temp-index `j` to the index of String `t` in the input-String,
                //   only looking at the trailing portion after index `j+1`
        >=0;)   //   And continue looping as long as long as that substring is found
      if(++c>m){//    If the temp-count + 1 is larger than the current largest count:
        m=c;    //     Set this current largest count to this temp-count + 1
        r=t;}   //     And the result-String to this temp-String
  return r;}    //  After the nested loops, return the result-String
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), ~~49~~ 46 bytes
*-3 bytes thanks to SirBogman*
```
{$^n;bag($^a~~m:ov/.**{$n}/X~$).max(*{*}).key}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WiUuzzopMV1DJS6xri7XKr9MX09Lq1olr1Y/ok5FUy83sUJDq1qrVlMvO7Wy9n9xYqVCmkKNSrxCWn4Rl4a6b2ZxMQgVFGSq6yiYaOoAxUIyUhWcE0sUioE4P0@hBMjNTSwBShtr6vwHAA "Perl 6 – Try It Online")
### Explanation
```
{                                            }  # Anonymous block
 $^n;  # Declare argument
         $^a~~m   /       /  # Regex match input string
               :ov           # Also return overlapping matches
                   .**{$n}   # Match n-character strings
                           X~$   # Stringify matches
     bag(                     )  # Convert to Bag (multiset)
                               .max(*{*})  # Find Pair string=>count with max value
                                         .key  # Get key (string)
```
[Answer]
# [PHP](https://php.net/), ~~133~~ 126 bytes
```
for(;$k<=strlen($i=$argv[1])-$j=$argv[2];)$s[]=substr($i,$k++,$j);$c=array_count_values($s);asort($c);echo array_key_last($c);
```
[Try it online!](https://tio.run/##LYrBCsMgEAV/xcMelKSH5GqkHxKCbMXWRhuDq4F8vbWkh/dghtndXqf73v4ZE5fgJ0U5BbtxeCvA9DrmYRE3WP8wLlIAzYui8mhhq3rwXdfDKiQYhSnhqU0sW9YHhmKJAwmJFFPmYIS0xkV2Vd6eOiBdvtaanWUGM6O2uLEffjDX8Qs "PHP – Try It Online")
Annoyingly `asort()` ~~and `end()`~~ can't be chained into one expression.
-7 bytes thanks to @manatwork
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 92 bytes
```
s=>n=>s.map(F=t=>([F[w=(w+t).slice(-n)]=-~F[w],w]),w="").sort(([a],[b])=>a-b)[s.length-1][1]
```
[Try it online!](https://tio.run/##fVFNa@MwEL37V0x7KBKRHfJRQgPyoYHsXva2sAfXbGRnEqsokpGUuHvZv54d293uUmjBlpg3b9488Z7VRYXa6zam1u3xepDXIHMr85CdVMu2MsqcFduik6ybRJ4Fo2tkqeWlTH8TXIqu5KKTt7fUcz4yVqhSFFXJZa7SihchM2iPsUlnZTErr7WzwRnMjDuy3Vc0xgn44bzZ3wiYQQ4GmGpbVD7AAqI@YeDJQLuBTYPotSPinIgEvDFjR6Z48r1B2KgIgX5nIVJ5UnGkE/SP3njEv@IfDS3GIeeBEFa52ECn6YheaaPtEUKragwZPPatUXo0AilYFxFqFTANaIOO@qLjLwgO@m3K7oc1yiPs9eGAHi3tP1chehImT990CP3XtlrAkoz0xbvninFJbMhkrxY7B66uz57UyBe4C3qjWkBVN0AW0fMdRRrrhk1ZNuEC2NN@wkm8r@CJTY98iDz02XtsDT3vI6pgP4USlagpZS8PrMiyTJWcTSrO7@4858n/ORczMRcLsRT3YrYshyUvMh@ndo/V@qSep/NkO9z3yRvwZf0wXbyWq9f2gtCTtqtksw7nsGTVSjzwT4phajx35O@Fc379Aw "JavaScript (Node.js) – Try It Online")
Takes `s` as a list of characters in `(s)(n)` syntax.
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), ~~97~~, ~~96~~, 92 bytes
```
s=>n=>s.Skip(n-1).Select((_,i)=>s.Substring(i,n)).GroupBy(x=>x).OrderBy(g=>g.Count()).Last()
```
[Try it online!](https://tio.run/##lY3BbsIwDIbve4rAyZFCJNhuW3JYJQYa0w5M4jh1xRSLzqniVIKn70LZbr1MsvXb/j/blcwqoX7ZcfUkKRLXZqiJk1m/xNC1efTnqJt679VBuV6cZ@fFbk/UAs/m2m6xwSoBfBrSg9N93VaADGtth4PPFzg7f9b2Pe4x5q52vrZF6DhBZjalZO0f73aREm6IEQ4wXWHTBKN2ITb7yVRDfvaKFz1GTVRxRIwUrtxilPs4oirKpCRnYJVy@12m/@P3o/gbiVyjbSkzD79M/wM "C# (Visual C# Interactive Compiler) – Try It Online")
Explanation:
```
s.Skip(n-1).Select((_,i)=>s.Substring(i,n)) //select every substring of size n
    .GroupBy(x=>x)                          //group by value
        .OrderBy(g=>g.Count())              //order by increasing recurrence
            .Last()                         //return the last one
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 28 bytes
```
ＮθＦ⊕⁻Ｌηθ⊞υ✂ηι⁺θιＵＭυ⟦№υιι⟧⊟⌈υ
```
[Try it online!](https://tio.run/##FYy7CgMhFET7/QrLK5gu3ZZbLcQgpAwpjDHxgq9Vb8jfG4UpDsOZMU4Xk7TvfY@Z2pXC0xY4@Lq8U2GwR1NssLHZF0iMVOFi46c5cFywg3POFFUHJNjNo7HgBEPBlB/iMXAI6yJ13lIIOr6md98SxTYJxwU@hqAKjkalDFL/MFAAmsPez4vEWmdyxn76@j8 "Charcoal – Try It Online") Link is to verbose version of code. Outputs the lexicographically largest substring with the highest count. Explanation:
```
Ｎθ
```
Input the desired substring length.
```
Ｆ⊕⁻Ｌηθ⊞υ✂ηι⁺θι
```
Extract all substrings of that length and push them to a list.
```
ＵＭυ⟦№υιι⟧
```
Replace each substring with a tuple of its count and the substring..
```
⊟⌈υ
```
Print the substring with the highest count.
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-g`](https://codegolf.meta.stackexchange.com/a/14339/), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
ãV ü ñÊÌ
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LWc&code=41Yg/CDxysw&input=IlRoZSBDYXQgc2F0IG9uIHRoZSBtYXQiCjM)
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 30 bytes
```
#&@@Commonest[##~Partition~1]&
```
Takes an array of characters, returns an array of characters.
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2ar8V9ZzcHBOT83Nz8vtbgkWlm5LiCxqCSzJDM/r84wVu2/pjVXQFFmXomCg0JatHNGYlFicklqUXE0F5eSU5JVbmKWvhGXG5g25YILuFtZ6htDueZQaWOgaG5mnjmXs1VxabGJRpK5jqUmHg5YF4RU4uKK1eHiMgVS/wE "Wolfram Language (Mathematica) – Try It Online")
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 36 bytes
```
#&@@Commonest[##~StringPartition~1]&
```
Takes a string, returns a string.
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2ar8V9ZzcHBOT83Nz8vtbgkWlm5LrikKDMvPSCxqCSzJDM/r84wVu2/pjVXAFC0RMFBIS2ai0vJKckqNzFL34jLDUybcsEF3K0s9Y2hXHOotDFQNDczz5zL2aq4tNhEI8lcx1ITDwesC0Iq6XBxmXJxxf4HAA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Zsh](https://www.zsh.org/), 89 bytes
```
local -A m
repeat $#1 ((++m[${1:$[i++]:$2}]))
for k v (${(kv)m})((v>M))&&r=$k&&M=$v
<<<$r
```
[Try it online!](https://tio.run/##fVFdT8IwFH2/v@KCdW0zDWFACAszURL1hfjCG/IwoHOTdZ1tmYlkv30WnPBGc9t77zn9ODn9MWmTMM6aXG3iHO8fUYIWpYgtkps@Mub7ckkO/ZAsM99fhSSoV5xDojTusEJGDmxXcVlzxqqHOeeepyOy87x5RCqYTqdENxxgkyq9NRF9Wocy/uwF8HzKIzgDL@GkN2jbcUsPHCqzYgyz0OzNkK3HdxN@pTmdalcKJ40GC2RAjZLCplnxgVlhhRbGupriAOiryHOF30rn2w7Ffgt0cJYKrTPlsADoIhW4cY4YN1WB1rUytlcpd/U8M@YYZZlRHAL5M8G98V8F5@rCji77hsDxAACldqIT7C6cbMQQb42L9@Jtb8u9DbGL7g@@fG5qJAUkSMwxH71HGrnhnKih@QU "Zsh – Try It Online")
```
local -A m                    # associative array, "local" is shorter than "typeset"
repeat $#1 {                  # looping for the length of the string will cause the
                              # program to use shorter, invalid strings as
                              # the bash-style slice gets cut short
    ((++m[${1:$[i++]:$2}]))   # increment the map at the key with substring
}                             # starting at $[i++] with length $2
for k v (${(kv)m}) {          # loop over the associative array's keys/values
    ((v>M)) && r=$k && M=$v   # find the max value M, save the key as r.
}                             # looping forward ensures we avoid our invalid subtrings
<<<$r                         # print $r
```
[Answer]
# [C++ (clang)](http://clang.llvm.org/), ~~201~~ \$\cdots\$ ~~151~~ 150 bytes
```
#import<map>
using S=std::string;S f(S s,int n){std::map<S,int>m;S t,r;for(int i=0,x=0;i<=s.size()-n;r=x<++m[t=s.substr(i++,n)]?x=m[t],t:r);return r;}
```
[Try it online!](https://tio.run/##dVBNT8MwDL33V5hyadV0Gh@nJS2HXbhwKhIHtkNo0y1Sm1SJiyam/vaRZIMxEFYi2@89O3bqYcjrjqvN4XAt@0EbZD0fymi0Um2gKiw2i4VF4zJaQZtUYIlUCCrdB8qJWeWRsnc8EkNbbRKvkMWc7Io5laywMys/RJLmippix7Ksf0UPjm@ucyKzjKh0/bArHLwmuDApNQJHo8DQyY2l6m5sBDCpnVzwvozO2LuoUZsy8i/2XKokjfYROAvTHVkW4oHLU3RcJwxdAgqLFgo4Vnnbx4@i6zSBF2265iomcDOR3@wVLLdCGKk9f3vBP0lr/RkG6bj7C@55K2DJEay7WgG6tOf4p8W/srspqCYanPtqSPiIOmyxCKuk323CrrUeERiDeBXH3nvNrJXGfqEEzrgVtVaNxyHPSziVtMm5iPzQpacOKxXTaDp8Ag "C++ (clang) – Try It Online")
# Ungolfed
```
#include<map>
#include<string>
std::string f(const std::string& s,int n) {
    std::map<std::string,int> m;
    for(int i=0;i<=s.size()-n;++i) {
        m[s.substr(i,n)]++;
    }
    int x=0;
    std::string r;
    for(auto a:m) {
         if(x<a.second) {
             x=a.second;
             r=a.first;
         }
    }
    return r;
}
```
[Answer]
# Excel (Ver. 1911), 60 Bytes
```
B2 'Input: String
B3 'Input: Sub-string Length
C2 =-COUNTIF(D2#,D2#)
D2 =MID(B2,SEQUENCE(LEN(B2)),B3)
E2 =SORT(C2#:D2)
F2 'Output
```
### Test Sample
Note: Newlines do exist in cells, but default formatting doesn't show them. Also only 10 of the 105 rows are shown to keep the image a decent size.
[![Test sample](https://i.stack.imgur.com/4X3rJ.png)](https://i.stack.imgur.com/4X3rJ.png)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
I have a feeling there may be an elusive 5-byter out there.
```
ṡċ@Þ`Ṫ
```
**[Try it online!](https://tio.run/##y0rNyan8///hzoVHuh0Oz0t4uHPV////1X0zi4tBqKAgU/2/CQA "Jelly – Try It Online")**
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 72 bytes
```
param($s,$n)(0..($s.Length-$n)|%{$s|% s*g $_ $n}|group|sort C*|% N*)[-1]
```
[Try it online!](https://tio.run/##hVLfT8IwEH7vX3EuxW6kgDAIssSESII@qC@a@GAMDqkws19pSzRh/O147QZiMnXZ2rvvvvt2d22efQipViKOd/QNLmCzy0MZJi5VnKaee9Zuo9m@EelSr1qIFI0NVUUDVHMJdAY03RZLma3zQmVSw6SJobum99TqPu@2hFAtlL7XMkqXqD12yOU8SML3To9M7T74Bq6CUcev3GEV9hFNonRIJoFaq747H/KR94djs6rVGRMydgngw112Gyll3jyPGIc@B2Y8NHteRTkqlUOXw9hlAePOS@p46NfTeqgTJKhyXhv2S5UkZJxhSQyFhrXEvtVBxm@EgVWalhxL7djaDGLq69d3gbpIOh6RaWc/k2s89IzDYybjxQmzTbMYd/8n4wQmKyFklBmO6bg0DqSHlYBJqEHhl6Wg0U1CXXGt4f/PLUeFKLaHqBkV/sGDAhqwsdlUacnxvq0TXMVnLl61WKCpo0QovFx0VtKkUOtYI3CK99kk2Rwbc6hbhVtRetDwgn2SQ8h29wU "PowerShell – Try It Online")
Unrolled:
```
param($s,$n)
(
    0..($s.Length-$n)|%{
        $s|% substring $_ $n
    }|group|sort Count|% Name
)[-1]
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 58 bytes
```
->s,n{a=(0..s.size).map{|i|s[i,n]};a.max_by{|e|a.count e}}
```
[Try it online!](https://tio.run/##fYvdCoJAEIXv9ymG6CJhWvMPybKLgrrqCUxirZU2cJMmIXN9drOQLoPDnHO@4dyrrO7y@NBNV4S6EfFkxjlxUi9p8UKUjVGGEoU6bReiB89jVjdGGsFPt0o/QLZtRxDDeMnvUpxZ4iC4CB6CjxAgOH7KpThdGjDaQAl5Qgg6hZZ98miviD4q1ahfpN06iwpxtV22/XrAfmAXzW1vqOHw9npaKB2yTUQV@ZMsxLn1p3xXw30D "Ruby – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-lp`, 47 bytes
```
++$k{$_}>$k{$\}&&($\=$_)for<>=~/(?=(.{$_}))/g}{
```
[Try it online!](https://tio.run/##K0gtyjH9/19bWyW7WiW@1g5ExdSqqWmoxNiqxGum5RfZ2NnW6WvY22rogRRoauqn11b//2/C5ZtZXAxCBQWZ//ILSjLz84r/6/qa6hkYGvzXzSkAAA "Perl 5 – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-h`](https://codegolf.meta.stackexchange.com/a/14339/), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
ãV
ñ@è¶X
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LWg&code=41YK8UDotlg&input=IlRoZSBDYXQgc2F0IG9uIHRoZSBtYXQiCjM)
[Answer]
# [Go](https://golang.org/), 131 bytes
```
func f(s string,l int)(p string){x,i:=0,0
m:=map[string]int{}
for;i<=len(s)-l;i++{t:=s[i:i+l]
m[t]++
if m[t]>x{x=m[t]
p=t}}
return}
```
Takes as input a `string` and an `int` representing the string and the length of the substring respectively.
Some newlines can be replaced by semicolons if people prefer one-liners.
[Try it online!](https://tio.run/##jVFNa8QgEL37K6Y5JcQu249Ttvayl14KPRR6CKGINVmpUdFZSAn@9lQ33bK9VXQYfW/GeTODXRwXn3yQMHJlCFGjsx6h6Ecslv5oBPRlgIBemYFqUAar0v3cq3miqmFbuiVjw0bu2vW9S6w5kt76nXpgWpoyVNd6p@p6xoaFVjWq1h0ZW@zqmqgesvc4zRPLDnEMYyRe4tGbuOCXk4AyoOBB5o@PAmEmAHAuK/tTroxEQk4lZylltbLOoQEaBm33m@kEpjUXT1JrS@HNev1xVVC4ifQvdgX7g5Re2YzeXqCvBwl7jhDSsQbwkLuI/yXdXZCeVQh5O6cScr8iSU6yqY3wTgFFFuC5GeSFprOKNK7NS2oGalOioGloKDYhR22mqtqt2eLyDQ "Go – Try It Online")
]