Given a list of integers and a chunk size, split the list into sublists where each sublist has at most chunkSize elements. The last sublist may have fewer elements if the total count is not evenly divisible by chunkSize. Return an empty list for empty input.

Examples:

Input: items = [1, 2, 3, 4, 5], chunkSize = 2 Output: [[1, 2], [3, 4], [5]] Explanation: Split into groups of 2, last group has 1 element Input: items = [1, 2, 3], chunkSize = 3 Output: [[1, 2, 3]] Explanation: All elements fit in one chunk Input: items = [1, 2, 3, 4], chunkSize = 1 Output: [[1], [2], [3], [4]] Explanation: Each element is its own chunk Input: items = [], chunkSize = 2 Output: [] Explanation: Empty input returns empty list Input: items = [1, 2, 3, 4, 5, 6], chunkSize = 3 Output: [[1, 2, 3], [4, 5, 6]] Explanation: Two equal chunks of 3
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay June 2026

Contest runs June 1 - 30. Complete challenges to climb the leaderboard!

Only the 30 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In