Set theory is an extremely important mathematical concept for any developer. The data that programs work with are often represented as sets, which means that the rules of set theory apply to them. This primarily concerns various operations on sets, such as intersection or union.
It doesn't mean that you have to know this theory like the back of your hand. On the contrary, learning its basic concepts and some operations is enough. It's most of what you need to easily solve the vast majority of problems. Set theory itself refers to certain intuitive concepts. It's fairly close to common sense and is understandable to people even if they don't have much mathematical education.
Brief description of terminology
The basic concept of set theory, surprisingly enough, is the set. A set is an arbitrary group of objects considered as a whole. The simplest example is numbers. The set of all numbers includes 10 elements (from 0 to 9).
But not every group of objects can be called a set. There's an important condition – all elements in the set must be unique. For example, the numbers 1, 1 and 3 cannot be called a set, but 1, 3, 5 can.
Sets can have a certain relationship with each other. For example, the set of natural numbers is a subset of integers, which in turn is a subset of rational numbers, and so on. The concept of “subset” means that all elements in one set are also part of another set, called a superset.
Sets can be represented quite conveniently using circles. You can quickly see how different sets relate to each other.
But mathematical objects such as numbers are not the only possible objects in sets. A set can be a group of people standing at a bus stop waiting for their bus, or the occupants of apartments in the same building, or the people in a city or country.
In programming, arrays and tables in a database can act as sets. JavaScript has a built-in Set mechanism for representing sets. But to work with it, you need a little understanding of object-oriented features, which will be covered in later courses.
Operations with sets
In practice, representing data as sets is useful when we want to do something with it. A simple example. When you go to another person's page on a social network, the social network shows you a section where you can see your friends. If you assume that your friends and your friend's friends are two sets, then mutual friends refer to the set obtained as the intersection of the original sets of friends.
Intersection is one of the prime examples of a set operation that is ubiquitous in programming. The same can be said for some other operations. All these operations must result in sets, as it means they'll obey the same rules as the original sets. For example, the uniqueness of elements is preserved.
Intersection
An intersection of sets is a set that includes elements that occur in all given sets at the same time.
An example involving mutual friends:
// One person's friends
const friends1 = ['james', 'mary', 'robert'];
// The other person's friends
const friends2 = ['patricia', 'robert', 'john', 'james', 'susan'];
// Mutual friends
// This function accepts any number of arrays.
// I.e., you can find the intersection of any number of arrays in a single call
_.intersection(friends1, friends2); // ['james', 'robert']
Union
A union of sets is a set that includes elements in all given sets.
const friends1 = ['james', 'mary', 'robert'];
const friends2 = ['patricia', 'robert', 'john', 'james', 'susan'];
_.union(friends1, friends2); // ['james', 'mary', 'robert', 'patricia', 'john', 'susan']
Each friend in the association is seen exactly once.
Complement
The complement of two sets is the set that includes elements of the first set that are not included in the second set. In programming, this operation is often called diff (difference).
const friends1 = ['james', 'mary', 'robert'];
const friends2 = ['patricia', 'robert', 'john', 'james', 'susan'];
_.difference(friends1, friends2); // ['mary']
Belonging to a set
The built-in includes()
method can be used to check if an element belongs to a set:
const terribleNumbers = [4, 13];
if (terribleNumbers.includes(10)) {
console.log('woah!');
}
Recommended materials
- Stories about Sets
- includes method
- intersection (Lodash)
- union function (Lodash)
- difference function (Lodash)
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.