Sets in Golang

Learn via video courses
Topics Covered

Overview

A Set in Golang is an abstract data structure that is used for storing a value without maintaining any particular order and avoids duplicate values. The Go language doesn’t have any sets by default like we have in other languages but there are ways of implementing a golang set.

Introduction to Set in Golang

A Set in Golang is a data structure that is used for holding a distinct collection of elements. It is very useful if we don’t want duplicate elements inside the data structure. Below is an example that shows a set describing animals present in a zoo.

 Introduction to Set in Golang A set doesn’t have key-value pair like maps.

Using Maps to Implement Sets

Although the Go language doesn’t have the set data structure in-built, we can use the map data structure and implement the set using it. We know that set is just a collection of elements without the key-value pair that we have in a map. The map can be represented with an empty struct value. Below is the implementation of a set by using a map in the Go language.

Output:

Run Code

Explanation: In this example, you will see that all the operations are executed that are the characteristic of a set will a time complexity that is the same as that of a set O(1) by using a map in Go language.

Bitset Implementation

A bitset is considered a relatively small set of integers and boolean values. These are often called flags that are represented in form of bits by using a single number.

Iterating Through Set Elements

We can iterate through set elements by using the basic for loop as shown in the example given below

Output:

Run Code

Explanation: In this example, the set is initialized as a map of a string with a void, and elements are added as a member. For iterating over the set elements, for loop is used by using the range keyword and the elements are printed at the output.

Adding Methods to the Set

Once the golang set is declared, the elements in the set can be added. This can be better understood by looking at the example given below

Output:

Run Code

Explanation: This is how the elements in the set are added, if you want to check whether the elements are added or not then you can iterate over the set through for loop. For this, check the previous example.

Golang Delete Set Element

We can also remove elements from the set by using the Delete method. To understand this better, look at the example given below

Output:

Run Code

Explanation: In this example, we have removed the element apple from the set. The Delete method takes the parameter as the name of the set as well as the element that is to be removed and removes the element from the set.

Golang Check If Element Exists

Write a program to check if the element exists or not in the set

Explanation: The syntax or the code snippet given is used to check if the element exists or not in the set. If it does then the code prints the element exists else it prints an element not found at the output.

Concurrency safe version

The first version is not concurrency safe because the routine of the golang might add an item or element in the set while another routine might be getting the list of elements or the size of the set. The code given below adds sync.RWMutex to the data structure and makes it concurrency safe.

Package set creates an ItemSet data structure for the Item type

Explanation: The implementation is simple. We can add a lock or unlock by adding a defer keyword. We can either add a lock inside the struct which will be more than one struct in the same file or add a generic type in Itemlock which is the local name.

Creating a concrete set data structure

For creating a concrete data structure, install genny if you haven’t already.

Run the command given below

Package set creates a StringSet data structure for the string type

Compatibility and Performance

The Go language doesn’t have any set data structure however it can be implemented by using the map in the golang. When it comes to semantics, we can use has, remove, and add methods on the set that we implemented. The only drawback of this is that we would have to write a custom implementation code for the set.

The performance that we achieve by implementing the set by using a map is the same as that of the original map. Here are a few performances that we were able to achieve:

  • O(1) time complexity for adding, removing as well as verifying the membership of the elements.
  • Due to the struct{ } type having a size of 0, space complexity also remains the same.

Conclusion

  • A set is an abstract data structure that is used for storing a value without maintaining any particular order and avoids duplicate values. The Go language doesn’t have any default sets like in other languages, but there are ways of implementing a set in Golang.
  • A set is a data structure that is used for holding a distinct collection of elements. It is very useful in case we don’t want any duplicate elements` inside the data structure.