Dev Helpers
← Back

Balanced bin allocation

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

Open this example in the interactive library

Live preview

Source code

// components/BalancedBinAllocation.tsx

export default function BalancedBinAllocation({
  counts = [12, 0, 5, 18],
}: { counts?: number[] } = {}) {
  const total = counts.reduce((a, b) => a + b, 0);
  return (
    <ol>
      {counts.map((_, i) => (
        <li key={i}>
          {Math.floor(total / counts.length) + (i < total % counts.length ? 1 : 0)}{" "}
          units
        </li>
      ))}
    </ol>
  );
}