> For the complete documentation index, see [llms.txt](https://frost.gitbook.io/iosbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://frost.gitbook.io/iosbook/leet-code/leetcode-solutions/15.-3sum.md).

# \[LeetCode] 15. 3Sum

Given an array `nums` of *n* integers, are there elements *a*, *b*, *c* in `nums` such that *a* + *b* + *c* = 0? Find all unique triplets in the array which gives the sum of zero.

**Note:**

The solution set must not contain duplicate triplets.

**Example:**

```
Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
```

**Analyse:**

**Solution:**

```swift
class Solution {
    
    func threeSum(_ nums: [Int]) -> [[Int]] {
        var output: [[Int]] = [];
        var sortedNums = nums.sorted{$0 < $1}
        var i = 0
         
        while (i < sortedNums.count - 2) {
            let value = sortedNums[i]
            var left = i + 1
            var right = sortedNums.count - 1
            
            while(left < sortedNums.count && right > 0 && left < right) {
                let sum = value + sortedNums[left] + sortedNums[right]
                if (sum == 0) {
                    output.append([sortedNums[left], sortedNums[right], value])
                    
                    repeat {
                        left += 1 
                    } while(left < right && sortedNums[left] == sortedNums[left - 1])
                      
                    repeat {
                        right -= 1 
                    } while(left < right && sortedNums[right] == sortedNums[right + 1])

                } else if(sum < 0) {
                  left += 1
                } else {
                  right -= 1
                }
            }
            
            repeat {
              i += 1
            }
            while((i < (sortedNums.count - 2)) && (sortedNums[i] == sortedNums[i - 1]))
        }
        
        return output 
    } 
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://frost.gitbook.io/iosbook/leet-code/leetcode-solutions/15.-3sum.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
