One of the most common tasks when working with slices is removing any duplicate elements. In this tutorial, we will walk you through the methods that you can use to accomplish this task.
Golang Slices
Before we dive into the process of removing the duplicates from a slice, let us explore the basics of slices in case you are not familiar.
What Is a Slice?
In Go, a slice is a dynamically-sized, flexible view into an underlying array. It is a more versatile alternative to arrays as it can grow or shrink as needed without requiring us to specify a fixed size.
We can use the slices to represent the sequences of data such as lists, collections, or arrays with dynamic sizes.
A slice is comprised of three main components in Go:
- Pointer to the underlying array
- Length (number of elements in the slice)
- Capacity (maximum number of elements the slice that can hold without resizing the underlying array)
The following shows the basic syntax of declaring a slice in Go:
Basics of Slices in Go
The first and most common method of creating a slice in Go is using the literal definition as shown in the following example:
We can also use the βmakeβ function to declare a new slice as demonstrated in the following example:
This should create a slice of integers with the length of 3 and a capacity of 5.
We can also create a slice by slicing an existing array as follows:
slice := arr[1:4]
This should create a new slice from the second and fourth elements of the array.
Adding Elements to Slice
In Go, we can add the elements to an existing slice using the append() function as shown in the following example:
databases = append(databases, "Redis")
In the βappendβ function, we need to pass the existing slice and the new elements that we wish to add to the slice.
Accessing the Elements
We can access the slice elements using the elements index as shown in the following example:
This should return the first element in the slice.
Iterating Over a Slice
We can use a basic βforβ loop to iterate over the elements of a slice as shown in the following example:
fmt.Printf("Index: %d, Value: %s\n", i, db)
}
Remove a Duplicate from a Slice
Let us get into the meat of the tutorial and learn the methods that we can use to remove the duplicate elements from a slice.
Method 1: Using a Loop
The simplest method of removing the duplicates from a slice is to iterate over the elements and remove any duplicate values. We can then build a new slice from the remaining elements as follows:
import "fmt"
func main() {
slice := []int{1, 2, 2, 3, 4, 4, 5}
unique := []int{}
for _, num := range slice {
found := false
for _, u := range unique {
if num == u {
found = true
break
}
}
if !found {
unique = append(unique, num)
}
}
fmt.Println(unique)
}
This method involves nesting the multiple loops to iterate and remove the duplicate elements from the slice.
As such, it has a complexing of O(n^2) which makes it very inefficient in large datasets.
Method 2: Using a Map
For a more efficient approach, we can take advantage of the map to keep track of unique elements. Consider the following example:
import "fmt"
func main() {
slice := []int{1, 2, 2, 3, 4, 4, 5}
unique := map[int]bool{}
result := []int{}
for _, num := range slice {
if !unique[num] {
unique[num] = true
result = append(result, num)
}
}
fmt.Println(result)
}
Unlike the nested βforβ loop approach, this one has a constant-time complexity which makes it significantly efficient.
This method also allows you to preserve the order of the original elements.
Conclusion
In this tutorial, we learned about the workings of slices in Go. We covered how to use a βforβ loop and maps to remove the duplicate values from the slice.