Dev Helpers
← Back

Consecutive run compression

Import and render <ConsecutiveRunCompression /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/ConsecutiveRunCompression.tsx

export default function ConsecutiveRunCompression({
  entries = ["Beta", "Alpha", "Gamma"],
}: { entries?: string[] } = {}) {
  const runs: { value: string; n: number }[] = [];
  for (const value of entries) {
    const last = runs.at(-1);
    if (last?.value === value) last.n++;
    else runs.push({ value, n: 1 });
  }
  return (
    <ol>
      {runs.map((r, i) => (
        <li key={i}>
          {r.value} × {r.n}
        </li>
      ))}
    </ol>
  );
}