ðŸš€ Get 25% off access to all my premium courses - use discount code FUNCMAIN at checkout - view the pricing page now!

# Challenge 11 - Sets and Subsets

ðŸ‘‹ Welcome Gophers! In this challenge, you are tasked with trying to implement a function that checks to see if a set is a sub-set of another set.

We’ll be carrying on the flying theme where the function takes in a `slice` of Flights and then checks to see if they exist within another `slice` of flights.

Hint

There are a number of ways to solve this. You may be able to use the `reflect` package or you may be able to serialize each flight and create a hash of them which you can store in a hash.

View Solution
``````package main

import (
"bytes"
"encoding/gob"
"fmt"
)

// Flight struct which contains
// the origin, destination and price of a flight
type Flight struct {
Origin      string
Destination string
Price       int
}

// IsSubset checks to see if the first set of
// flights is a subset of the second set of flights.
func IsSubset(first, second []Flight) bool {
// implement
set := make(map[Flight]int)
for _, value := range second {
set[value] += 1
}

for _, value := range first {
if count, found := set[value]; !found {
return false
} else if count < 1 {
return false
} else {
set[value] = count - 1
}
}

return true
}

func Hash(f Flight) []byte {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(f)
return b.Bytes()
}

func main() {
fmt.Println("Sets and Subsets Challenge")
firstFlights := []Flight{
Flight{Origin: "GLA", Destination: "CDG", Price: 1000},
Flight{Origin: "GLA", Destination: "JFK", Price: 5000},
Flight{Origin: "GLA", Destination: "SNG", Price: 3000},
}

secondFlights := []Flight{
Flight{Origin: "GLA", Destination: "CDG", Price: 1000},
Flight{Origin: "GLA", Destination: "JFK", Price: 5000},
Flight{Origin: "GLA", Destination: "SNG", Price: 3000},
Flight{Origin: "GLA", Destination: "AMS", Price: 500},
}

subset := IsSubset(firstFlights, secondFlights)
fmt.Println(subset)
}

``````