Understanding Set Theory

Sukhrob Golibboev
4 min readMay 18, 2019

W e are going to talk about a data structure called SET, you might already have used in your middle or high school math classes or even daily life. It is a very simple and useful concept in mathematics. The mathematical definition of a set according to Wikipedia — a set is a collection of distinct items. For example, the numbers 2, 4, and 6 are distinct objects when considered separately, written as {2, 4, 6}. Since we are software engineers and write code, let's look at set implementation, its usage, and operations in a computer science standpoint.

Set Operations. Credit: Online Math Learning

Set is Abstract data type. talk about sets are useful when you are working with data at the order or frequency of the values do not matter.

Set Analogy with Array

If you are studying Computer Science(CS) or write code at all with an introductory level. I am sure if you write code at all you probably have used a list or array data structure depending on what programming language you use. As we know array[insert a link to documention] is ordered list of elements. You can access the item by its index. Unlikely, the set has an arbitrary order which means there is no specific order and you can’t have duplicate elements in a set.

Set is a list of unordered distinct elements

Set Analogy with Dictionary

Another analogy can be made with a dictionary. As we know at this point set is a collection of unordered distinct elements. You can think set of elements as of a dictionary`s keys. You might be saying a dictionary is key and value pair. Yes, since set stores single elements we can’t have both key and value from the dictionary. since we know keys being unique is guaranteed but a dictionary can have duplicate values. we can say the keys of a dictionary are a set of unique unordered elements. if we extract the keys from the dictionary and it becomes a list of unique elements as the keys of a dictionary are guaranteed to be unique. When you try to add duplicated key into a dictionary it doesn’t let you add duplicate keys. Neither is set. You can’t have a duplicate element in a set. Set is like a dictionary but the values are none.

Set Operations

Now we know what set is but what do we use it for? or what operations we can do with it?. There are three main methods or operations we can do. Union, difference and intersection.

Union

Union — is the collection of all items from the sets. It is denoted for sets A and B as following AUB. Let's say, there are two classes, math, and physics. Then we want to collect all student names into one roster. It is the union of all math and physics class students.

Set Union. Credit: steemit.com

Intersection

Intersection — is the items the mutual items both sets have and denoted as if there are sets A and B — A∩B. There is a really good real-life example of this when you receive a friend request on Instagram, you first want to check what mutual friends you both have if you have mutual friends then you probably know that person. but it does not show all of his and your followers if the account is private. So all the mutual friends are the intersection between your account and the new friend's account have.

Set Intersection. Credit: steemit.com

Difference

The difference — is all the items only belongs to one set. And denoted as it is saying A-B for sets A and B. A good example would be, you have one group of friends who like chocolate ice-cream and another like vanilla. And there are some friends from both groups who like both. And you brought a box of ice-cream and asked to come friends only who like chocolate, not both as the chocolate ice-cream was a limited number.

Set Difference. Credit: GeeksforGeeks

Conclusion

Now you are in the set of people who know how what set is. Thank you for reading and if you find anything you want to add feel free to leave a comment down below.

Resources:

--

--