Premium Content - Free Preview
Sets are an abstract data type for holding an unordered collection of items without any duplicate values. Unlike Arrays, Sets have no specific order or index associated with each item.
These are the important methods a Set supports:
- add(item) - Adds an item to the set.
- remove(item) - Removes an item from the Set.
- contains(item) - Returns true if the item is in the Set, and false otherwise.
Purpose of Sets
The Set is useful when you want to check if items are in a collection but there's no specific order that you care about. For example, let's say you're given a raw list of numbers and need to find the numbers that appear more than once. What Algorithm could you use to find them?