cs-042 — Deduplication Patterns
| Field | Value |
|---|---|
| id | cs-042 |
| difficulty | Foundational |
| topics | lists, uniqueness |
| lecture | lecture-11 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | lectures 1-2 (problem framing, precise pseudocode) |
| objective | recognise the pattern, translate it into precise steps, and trace it correctly |
| solution | held by the instructor; discussed after the attempt |
Problem
Remove duplicates three ways: preserving order with a seen-set, via sorting, and the naive quadratic way. Compare and test all three.
Your deliverable is the three deduplication strategies applied to the worked instance, a one-line cost statement for each, and one input where the sorted approach visibly changes the result. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: [3, 1, 3, 2, 1, 3]
- Output: order-keeping: [3, 1, 2]; sort-based: [1, 2, 3]; naive O(n^2): same as order-keeping
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- Implement all three, then time them on 10k elements - the point is the comparison, not the answer.
- Slices are half-open: they make boundaries explicit instead of hidden in loop conditions.
- Aliasing is the difference between copying a list and copying a name.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "When is the extra memory of slicing worth it, and when would you insist on in-place?"
Constraints and assumptions
- Difficulty: Foundational (early-semester techniques assumed: precise statements, traces, simple loops).
- Topics: lists, uniqueness.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes; naming the pattern and stating the assumption is already a pass, completing the calculation is the stretch.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Convert your in-place solution to a copying one and compare the trade-offs.