Sets: How a Single Rule Can Affect Efficiency

Let’s explore another data structure: the set. A set is a data structure that does not allow duplicate values to be contained within it.

There are actually different types of sets, but for this discussion, we’ll talk about an array-based set. This set is just like an array—it is a simple list of values. The only difference between this set and a classic array is that the set never allows duplicate values to be inserted into it.

For example, if you had the set ["a", "b", "c"] and tried to add another "b", the computer just wouldn’t allow it, since a "b" already exists within the set.

Sets are useful when you need to ensure that you don’t have duplicate data.

For instance, if you’re creating an online ...

Get A Common-Sense Guide to Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.