Skip to content

Backtracking

How to avoid duplicates working with backtracking

Medium 90. Subsets II

Medium 40. Combination Sum II


One of the basic ideas for avoiding duplication involves sorting.

For example, we are given an array candidates = [10,1,2,7,6,1,5] and a target = 8.

How to find all unique combinations in candidates where the candidate numbers sum to target?

In here we have to avoid duplicates, for example, if we found [1,2,5] we have to avoid [2,1,5].

The solution is:

// sort an array
// inside the loop of the backtracking recursive function
if (i != start && array[i] == array[start]) {
    continue
}