Sets
In Volume 1, Chapter 1, I introduced the notion of a set. A set is a collection of unique values; there are no duplicate values. There are many different applications where maintaining a set can be useful, which I demonstrate in the following sections.
Finding Duplicates
Suppose we have an array of integers in which there may or may not be duplicate values, and we need a function that will tell us whether there are any duplicates. For example, if we have the array [4, 3, 5, 1, 7, 2, 6, 8], our function will return False because no duplicate values are present. However, if the array is [1, 2, 3, 1], the function will return True because there are two instances of the integer 1.
The brute-force approach to detecting duplicates would be to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access