diff options
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/utils.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index f184c0bbb..bf69fd30e 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -261,3 +261,41 @@ func AsJson(i interface{}) string { bytes, _ := json.MarshalIndent(i, "", " ") return string(bytes) } + +// UnionInt returns the union of two int arrays +func UnionInt(a, b []int) []int { + m := make(map[int]bool) + + for _, item := range a { + m[item] = true + } + + for _, item := range b { + if _, ok := m[item]; !ok { + // this does not mutate the original a slice + // though it does mutate the backing array I believe + // but that doesn't matter because if you later want to append to the + // original a it must see that the backing array has been changed + // and create a new one + a = append(a, item) + } + } + return a +} + +// DifferenceInt returns the difference of two int arrays +func DifferenceInt(a, b []int) []int { + result := []int{} + m := make(map[int]bool) + + for _, item := range b { + m[item] = true + } + + for _, item := range a { + if _, ok := m[item]; !ok { + result = append(result, item) + } + } + return result +} |