3Sum
Given an integer array nums, return all the unique triplets [nums[i], nums[j], nums[k]] such that:
• i != j
• i != k
• j != k
and
nums[i] + nums[j] + nums[k] == 0
The solution set must not contain duplicate triplets.
Example 1
Input
6 -1 0 1 2 -1 -4
Output
-1 -1 2 -1 0 1
Explanation
The unique triplets whose sum is 0 are:
[-1, -1, 2]
[-1, 0, 1]
Example 2
Input
3 0 0 0
Output
0 0 0
Explanation
The only valid triplet is [0, 0, 0].
Constraints
Hints:
Hint 1
Sort the array first so that duplicate triplets can be skipped easily.
Hint 2
Fix one element and use two pointers to find the remaining two elements whose sum equals the negative of the fixed element. Skip duplicate values to avoid repeated triplets.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 5, 2026
Last updated
Aug 6, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output