How to check whether a slice is subset of another slice in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

func isSubset(arr1 []int, arr2 []int) bool {
    for i := 0; i < len(arr2); i++ {
        found := false
        for j := 0; j < len(arr1); j++ {
            if arr2[i] == arr1[j] {
                found = true
                break
            }
        }
        if !found {
            return false
        }
    }
    
    return true
}

func main() {
    arr1 := []int{5, 1, 8, 12, 40, 7, 9, 100}
    arr2 := []int{8, 40, 9, 1}

    if isSubset(arr1, arr2) {
        fmt.Println("yes")
    } else {
        fmt.Println("no")
    }
}



/*
run:

yes

*/

 



answered Oct 4, 2025 by avibootz
...