Given a list containing only the values 0, 1, and 2, sort the list in ascending order so all 0s come first, then all 1s, then all 2s. Do not use the built-in sort() method. Use the Dutch National Flag algorithm with three pointer variables to solve this in a single pass.
Examples:
Input: nums = [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]
Explanation: All 0s first, then 1s, then 2s
Input: nums = [2, 0, 1]
Output: [0, 1, 2]
Explanation: One of each value sorted in order
Input: nums = [0]
Output: [0]
Explanation: Single element, no sorting needed
Input: nums = [1, 1, 0, 2, 0, 1]
Output: [0, 0, 1, 1, 1, 2]
Explanation: Two 0s, three 1s, one 2
Input: nums = [2, 2, 2]
Output: [2, 2, 2]
Explanation: All same values, no change needed
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 July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.