> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/zhong-yu-shua-dao-100-dao-le-wo-shi-fen-shui-ling/bu-chong-120-dao/40.-combination-sum-ii.md).

# 40. Combination Sum II

\# Medium

{% hint style="info" %}
Very similar to #39, the only difference is to keep avoid dupplicate combinations. Then should think how to filter the dupplication.&#x20;

Solution: sort the candidates list, then avoid repeating to use same element in list.

```python
if i > x and s[i] == s[i-1]:
                continue
```

{% endhint %}

{% tabs %}
{% tab title="Python" %}

```python
class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []
        sub = []
        candidates.sort()
        self.helper(candidates, sub, target, 0, result)
        return result
    
    def helper(self, s, sub, target, x, result):
        if target == 0:
            result.append(sub[:])
            return
        elif target < 0:
            return 
        
        for i in range(x, len(s)):
            if i > x and s[i] == s[i-1]:
                continue
            sub.append(s[i])
            self.helper(s, sub, target-s[i], i+1, result)
            sub.pop()
```

{% endtab %}
{% endtabs %}
