Math Fundamentals18 sections · 814 units
Open in Course

Subsets II - Insight

Skip duplicates

First sort the array. This groups duplicates together. When iterating, if the current element equals the previous one and we did not include the previous one, skip the current one too.

Why does this work? If you skip the first 2 but include the second 2, you get the same subset as including the first 2 but skipping the second. By always using the earliest duplicate, you avoid repetition.