Run-length encoding: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Encode each consecutive run as an ASCII code and repeat count.

Replace each maximal run of equal symbols with a symbol and its repetition count.

Open the 3D visualization

Run-length encoding example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Run-length encoding

Adjacent-repeat compression and reversible-transform teaching.

Choosing an alternative

Dictionary compression can exploit repeated phrases, not only runs.

Worked example: input and state changes

Example input

AAAABBBCC

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [],
      "active": []
    }
  2. Step 1: Encode run A repeated 4 times.

    State at this checkpoint
    {
      "values": [
        "A×4"
      ],
      "active": [
        0
      ]
    }
  3. Step 2: Encode run B repeated 3 times.

    State at this checkpoint
    {
      "values": [
        "A×4",
        "B×3"
      ],
      "active": [
        1
      ]
    }
  4. Step 3: Encode run C repeated 2 times.

    State at this checkpoint
    {
      "values": [
        "A×4",
        "B×3",
        "C×2"
      ],
      "active": [
        2
      ]
    }
  5. Step 4: Encoded runs: A×4, B×3, C×2.

    State at this checkpoint
    {
      "values": [
        "A×4",
        "B×3",
        "C×2"
      ],
      "active": [],
      "encoded": [
        65,
        4,
        66,
        3,
        67,
        2
      ]
    }

Expected final state

Encoded runs: A×4, B×3, C×2.

Complexity and input bounds

Family: Strings. Time complexity: O(n).

Space complexity: O(run count) output; O(1) counting state.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

One string of 1–40 ASCII letters.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Short runs may expand the data; the source uses packed code/count pairs rather than display notation.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
1
2
3
4
5
6
7
8
9
// 1–40 ASCII letter codes. Writes packed [letterCode,count] pairs; returns number of output integers (twice the run count). Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n, output) {
    let i = 0; let count = 0;
    while (i < n) {
        let j = i + 1; while (j < n && a[j] == a[i]) { j++; }
        output[count++] = a[i]; output[count++] = j - i; i = j;
    }
    return count;
}

Source

The RFC documents a byte-run instruction as an authoritative format example. It is not the original RLE publication, and the playground uses its own character/count representation.