Repaso
EspaƱol
Algorithms & DSFoundationsALG-53

What is special about a set?

Options

  1. Stores each value once; fast membership checks (Correct option)
  2. It always keeps its values in sorted order
  3. It can only store numbers, never text values
  4. It allows duplicates and keeps their order

Answer

It stores each value only once and checks 'is this in the set?' very quickly

Why

A set is a collection with no duplicates. Adding the same value twice leaves one copy.

Python: s = {1, 2, 2, 3} becomes {1, 2, 3}. Java: HashSet.

It is the usual tool for removing duplicates and for fast membership checks (x in s is O(1) on average, while in a list it is O(n)).

Remember

Set = unique values + fast membership check.

Key terms

Practice this question in Repaso