Return all unique triplets [a, b, c] from the list such that a + b + c = 0. Each triplet must be sorted ascending, and the outer list of triplets is returned in ascending order. There must be no duplicate triplets. Return an empty list for null input or fewer than 3 elements.
Examples:
Input: nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]
Explanation: These are the only two unique triplets that sum to zero
Input: nums = [0, 0, 0]
Output: [[0, 0, 0]]
Explanation: Only one triplet is possible and it sums to zero
Input: nums = [1, 2, 3]
Output: []
Explanation: No triplet sums to zero
Input: nums = []
Output: []
Explanation: Empty list returns an empty list
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 October 2026 (Oct 4–15)Sponsored by FLXBL
Contest runs October 4 - 15. 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 12 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.