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!
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 November 2026 (Nov 16–30)Sponsored by FLXBL
Contest runs November 16 - 30. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.