1 min read

Set Membership Tricks

Table of Contents

Approach

Store all seen items in a set, then short-circuit when you find a conflict or match.

Use cases

  • Duplicates detection
  • Longest streaks
  • Validating constraints

Tip

When values are contiguous or bounded, consider a boolean array for faster access.