Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Two Pointers
Google
Meta
3Sum

Find all unique triplets in an array that sum to zero.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums, return all unique triplets [nums[i], nums[j], nums[k]] such that:

  • i, j, and k are distinct indices
  • nums[i] + nums[j] + nums[k] == 0
  • The answer must not contain duplicate triplets

A triplet is considered the same as another triplet if it contains the same three values, regardless of index order.

Notes

  • The input array may contain duplicate values.
  • You may return the triplets in any order.

Input Format

  • A single integer array nums.

Output Format

  • A list of all unique triplets whose sum is zero.

Constraints

  • 3 <= nums.length
  • The array can contain negative, zero, and positive integers
  • The number of valid triplets may be large, so focus on eliminating duplicates correctly

Input Format

  • An integer array nums.

Output Format

  • Return a list of unique triplets [a, b, c] such that a + b + c = 0.

Constraints

  • 3 <= nums.length
  • Values may repeat
  • Triplets must be unique by value
  • Order of triplets does not matter
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [-1,0,1,2,-1,-4]

Output

[[-1,-1,2],[-1,0,1]]

Explanation

The unique triplets that sum to zero are [-1, -1, 2] and [-1, 0, 1].

Example 2

Input

nums = [0,1,1]

Output

[]

Explanation

No three distinct elements sum to zero.

Show 1 more example

Example 3

Input

nums = [0,0,0]

Output

[[0,0,0]]

Explanation

There is exactly one unique triplet, formed by the three zeros.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.