Golang program to remove a subset from a slice



Slice is similar to an array, the only difference is that an array is a fixed sequence of elements whereas slice the array elements are dynamic. This makes the slice more efficient and faster in various applications. In slice, the elements are passed by reference instead of values. In this article, we will learn different techniques to remove a subset from slice using go language programming

Syntax

func append(slice, element_1, element_2?, element_N) []T 

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 ? Create a package main and declare fmt(format package) package

  • Step 2 ? In the main function create a slice, and add values in that slice.

  • Step 3 ? Create the subset of slice and call the internal function

  • Step 4 ? Create an output slice and iterate over the slice

  • Step 5 ? Print the output

Example 1

In this example, we will see how to remove a subset from a slice using nested for loop.

package main import "fmt" func main() { slice := []int{10, 20, 30, 40, 50, 60} //create slice fmt.Println("The elements of slice are:", slice) subset := []int{30, 40, 50, 60} //create subset fmt.Println("The elements of subset which are to be removed are:", subset) slice = removesubset(slice, subset) // Call the removesubset function fmt.Println("The slice after removing elements subset from it are:") fmt.Println(slice) } // removesubset function func removesubset(slice, subset []int) []int { for _, val := range subset { for i, element := range slice { if val == element { slice = append(slice[:i], slice[i+1:]...) //remove subset using append function break } } } return slice //return slice to the function after subsets are removed } 

Output

The elements of slice are: [10 20 30 40 50 60] The elements of subset which are to be removed are: [30 40 50 60] The slice after removing elements subset from it are: [10 20] 

Example 2

In this example, we will use map to store elements of the subset using map which will be created using make function which is a built-in function in Golang.

package main import "fmt" func main() { slice := []int{10, 20, 30, 40, 50, 60} //create slice fmt.Println("The elements of slice are:", slice) subset := []int{30, 40, 50, 60} //create subset fmt.Println("The elements of subset are:", subset) slice = removesubset(slice, subset) fmt.Println("The slice after removal of elements is:") fmt.Println(slice) } func removesubset(slice, subset []int) []int { subsetmap := make(map[int]bool) //create subsetmap using make function for _, element := range subset { subsetmap[element] = true } var output []int for _, val := range slice { if !subsetmap[val] { output = append(output, val) } } return output //return output slice without subsets } 

Output

The elements of slice are: [10 20 30 40 50 60] The elements of subset are: [30 40 50 60] The slice after removal of elements is: [10 20] 

Conclusion

We executed the program of removing a subset from a slice using two examples. In the first example we used nested for loop and in the second example we used map to store the subset values.

Updated on: 2023-02-13T16:11:33+05:30

535 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements