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 September 2026 (Sep 16–30)Sponsored by FLXBL

Contest runs September 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.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In