golang

Removing Duplicates in Go (Remove Duplicates from Slice)

Slices are a fundamental data structure in Go as they allow us to work with collection of elements.

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:

  1. Pointer to the underlying array
  2. Length (number of elements in the slice)
  3. 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:

var slice_name []type

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:

databases := []string{"MySQL", "PostgreSQL", "Redis"}

We can also use the β€œmake” function to declare a new slice as demonstrated in the following example:

databases := make([]string, 3, 5)

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:

arr := [5]int{1, 2, 3, 4, 5}
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 := []string{"MySQL", "PostgreSQL"}
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:

i := databases[0]

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:

for i, db := range databases {

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:

package main
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:

package main
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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list