Golang program to check if a set is the subset of another slice


Slice is similar to the array, the only difference is that an array is a fixed sequence of elements whereas slice the array elements are dynamic. This makes slice more efficient and faster in various applications. In slice the elements are passed by reference instead of values. In this article, we are going to learn various techniques to check if a set of one slice is a subset of another slice or not using Go programming language.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a subset and superset slice where subset will be the small set and superset will be the large set.

  • Step 3 − Print the subset and superset on the console using print statement in Golang.

  • Step 4 − Call the function is_subset with two parameters subset and superset on which operations have to be performed.

  • Step 5 − In the function create checkset map using make function which is a built-in function in Golang.

  • Step 6 − Run a loop till the range of subset and keep adding the elements of the subset in the map with value true.

  • Step 7 − Run another loop till the range of superset and check if the superset element exists in the map remove that element from the map.

  • Step 8 − After the loop terminates return the length of checkset map, if the length id equal to zero this means the set is superset of another set.

  • Step 9 − The Boolean value will be printed on the console using fmt.Println() function where ln means new line.

Example1

In this example we will see how to check if the set is the subset of another slice using map which will be constructed using make function.

package main
import (
   "fmt"
)
func main() {
   subset := []int{10, 20, 30} //create subset
   fmt.Println("The elements of set are:", subset)
   superset := []int{10, 20, 30, 40, 50} //create superset
   fmt.Println("The elements of superset are:", superset)
   is_Subset := is_Subset(subset, superset)
   fmt.Println("The elements are present in superset or not?")
   fmt.Println(is_Subset)
}
func is_Subset(subset []int, superset []int) bool {
   checkset := make(map[int]bool)
   for _, element := range subset {
      checkset[element] = true
   }
   for _, value := range superset {
      if checkset[value] {
         delete(checkset, value)
      }
   }
   return len(checkset) == 0 //this implies that set is subset of superset
}

Output

The elements of set are: [10 20 30]
The elements of superset are: [10 20 30 40 50]
The elements are present in superset or not?
true

Example 2

In this example, we will check if a set is the subset of another slice using map and range. We will run one loop for the superset and one for subset and output will be printed as a Boolean value.

package main
import (
   "fmt"
)
func main() {
   subset := []int{10, 20, 30, 40, 50} //create subset
   fmt.Println("The elements in the subset are:", subset)
   superset := []int{10, 20, 30, 40, 50} //create superset
   fmt.Println("The elements in the superset are:", superset)
   is_subset := is_subset(subset, superset)
   fmt.Println("Is the set subset of superset?")
   fmt.Println(is_subset)
}
func is_subset(subset []int, superset []int) bool {
   checkset := make(map[int]bool)
   for _, element := range subset {
      checkset[element] = true
   }
   for _, value := range superset {
      if !checkset[value] {
         return false //return false if set is not subset of superset
      }
   }
   return true //return true if set is subset of superset
}

Output

The elements in the subset are: [10 20 30 40 50]
The elements in the superset are: [10 20 30 40 50]
Is the set subset of superset?
true

Conclusion

We executed the program of checking if the slice is subset of the superset using different set of examples. In the first example we used length of subset =0 approach and in the second example we used map and range approach and the output is either true or false. Hence, program executed successfully.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements