Sets

Learn to create sets, add and remove elements, handle duplicates, and work with mutable sets.

Introduction

We use sets instead of lists when:

  • We want to ensure that elements in our collection are unique (sets keep only unique elements).

  • We frequently look for an element in a collection (finding elements in a set is much more efficient than doing so in a list.

Note: The default set is based on a hash table algorithm, which makes finding an element with a properly ...