utils/comparisons.go

40 lines
1.2 KiB
Go

package utils
// GetSharedElementsOfStringSlices finds the elements that are in both slices
func GetSharedElementsOfStringSlices(a, b []string) []string {
// Create a map to store the elements of "b"
mapOfElementsOfSliceB := make(map[string]bool)
for _, e := range b {
mapOfElementsOfSliceB[e] = true
}
// Iterate through "a" and find the elements that are not in "b"
var sharedElementsOfBothSlices []string
for _, e := range a {
if mapOfElementsOfSliceB[e] {
sharedElementsOfBothSlices = append(sharedElementsOfBothSlices, e)
}
}
return sharedElementsOfBothSlices
}
// GetUniqueElementsOfStringSlices finds the elements that are in the first slice but not in the second slice
func GetUniqueElementsOfStringSlices(a, b []string) []string {
// Create a map to store the elements of "b"
mapOfElementsOfSliceB := make(map[string]bool)
for _, e := range b {
mapOfElementsOfSliceB[e] = true
}
// Iterate through "a" and find the elements that are not in "b"
var uniqueElementsOfSliceA []string
for _, e := range a {
if !mapOfElementsOfSliceB[e] {
uniqueElementsOfSliceA = append(uniqueElementsOfSliceA, e)
}
}
return uniqueElementsOfSliceA
}