VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 08: Recursion and backtracking

Lesson 8.4 of 10 in this module Unrolled Pro

Subsets II

All distinct subsets of a list that contains duplicates. From generating all 2^n subsets and de-duplicating with a set (wasted work that grows with the number of repeats), to sorting and skipping a repeated value at the same depth of the tree, to treating the input as (value, count) pairs and choosing how many copies of each. Then combinations from candidates with duplicates that sum to a target, counting distinct subsets without listing them, and unique augmentation recipes drawn from a pool with repeats.

about 10 min 4 Python examples 2 quick checks

Problem. Given nums that may contain duplicates, return all distinct subsets. Two subsets are the same if they contain the same values with the same multiplicities, whatever the order.

text
nums = [1, 2, 2]   →   [], [1], [2], [1, 2], [2, 2], [1, 2, 2]          (6, not 2³ = 8)
nums = [0]         →   [], [0]

Lesson 8.3's method would produce [2] twice (from either copy) and [1, 2] twice. The task is to never generate the duplicates in the first place.

ML context. Pools with repeats are common: a list of augmentations where some are listed twice to weight them, an ensemble pool with several identical model configs, a bag of tokens. "All distinct combinations" from such a pool must not waste time (or GPU runs) on repeats. And often you only need how many distinct combinations there are, which has a closed form (Change B).


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Subsets II"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee