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 accountHow 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
🏆 All Time Leaderboard
Contest runs January 1, 1970 - December 31, 9999. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.