Let’s Talk About Sets
A few months ago, I was extended the opportunity to complete a technical interview with a company who told me in advance that I’d be working with sets
. At first, I panicked. I had no idea what a set
was and wasn’t sure if I’d be able to grasp the concept in time for the interview.
Like any other developer, I quickly turned to Google and found that there were a scarce amount of articles that discussed sets
in a simple manner. When you are a brand new dev, those long articles with fancy language that go very deep into a topic are intimidating. You end up needing to google other terms and concepts which leads you down a rabbit hole and you spend way more time than was needed to understand the basics of the topic you began with.
Due to this, I strive to write my articles using simple language to avoid intimidating non-tech professionals and newbies. With all of this in mind, I have decided to discuss sets
here because they are something that nobody should be intimidated of and are quite useful! So let’s jump right in and get started.
What is a Set?
A set
is a data structure which contains any number of unique values that are unordered. You can think of a set
as a list/collection of elements. If you are familiar with arrays
you may be thinking “Oh! Just like an array!” In a way, yes it is similar to an array but there are a few differences that we will discuss later in this post.
Now when I said unique I meant that a set
does not allow duplicate elements. To dive a little deeper, let’s say that we have a list of numbers: 1, 2, 3, 4
. If we were working with a set
and wanted to add to this list, it wouldn’t add the number 1, 2, 3, or 4
again because they already exist within the list.
What languages utilize Sets?
Sets are a widely accepted data structure amongst many languages. Through my research, I have discovered that the following languages all make use of sets
as a data structure.
Now keep in mind that there may be other programming languages that make use of this data structure, so if you are wondering about a specific one that isn’t listed above just hop on your web browser and search!
Sets vs. Arrays
Earlier I brought up the fact that a set
is somewhat similar to an array
, so let’s go ahead and discuss some of the biggest similarities and differences.
Similarities
- Both
sets
andarrays
are a collection/list of elements. - We have the ability to add and delete items from both data structures.
- We can search each type for a specific element.
Note: There are other methods that can be used on both sets
and arrays
that are similar, but I’m not going to discuss every single one. Please feel free to reference my list of sources at the end of the article or spend some time searching Google, Bing, etc. yourself.
Differences
- The most notable difference between a
set
and anarray
is the uniqueness of aset
. Anarray
can hold any number of elements including duplicates. - Another notable difference between the two data structure types is that an
array
is an indexed collection whereas aset
is unindexed. This means that you do not have the ability to directly access an element using its index like you can with anarray
.
When would you use a Set?
You would want to use a set
when your end goal is to create a list/collection that includes only unique values. In many languages, like Ruby and JavaScript, you can even take an array
that you want to remove duplicates from and turn it into a set. You could then take that set and turn it back into an array without those duplicated elements if an array
is what you need as your end product.
In many languages, searching for something within a set
is faster than an array
. For instance, in Ruby using .include?()
on a set
is 11x faster than using .include?()
on an array
. I think that’s significant and makes a great case when it comes to using sets
for large collections of data.
In multiple programming languages that make use of set
there is also a class of SortedSet
, which is helpful if you need the collection in numerical or alphabetical order. This type of set
is useful because it will immediately put anything added to the set
in the proper sorted order.
From my research, I noticed that Ruby, C#/.NET, and Java all have SortedSet
. If you are curious if another language has it just search!
Final Thoughts
In this article, we’ve taken a look at what a set
is, how it compares to an array, and when to use one. I personally really like the idea of sets
but have yet to implement it myself outside of the interview that I talked about at the beginning of this post.
I have learned through my research that a set
performs significantly faster than an array
in some languages. This is really important because as developers we want to create fast, efficient code. A few of the articles that are listed in the sources section just below discuss the time complexity differences if you want to take a deeper look.
Thank you for reading and happy coding!!