Task. Given a list of token ids (a document or a stream) and n, count every n-gram (n consecutive tokens) and return the k most frequent, ties broken by the n-gram itself.
tokens = [5, 1, 2, 5, 1, 2, 5, 1], n = 2, k = 2
→ [((5, 1), 3), ((1, 2), 2)] ((2, 5) also appears twice; ties break by value, (1, 2) < (2, 5))
ML context. N-gram counts are everywhere in language-model work: classic n-gram language models and BLEU/ROUGE scores, repetition detection and n-gram blocking in decoding (lesson 4.3), near-duplicate detection with shingles, and contamination checks: does the training data contain long n-grams (often 13-grams) from a benchmark? At the scale of a pretraining corpus, how you slide and hash the window decides whether the job takes an hour or a week.