utils/comparisons.go

40 lines
1.2 KiB
Go
Raw Normal View History

2024-06-04 21:41:28 +02:00
package utils
2024-06-30 22:50:11 +02:00
// 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
}
2024-06-04 21:41:28 +02:00
// 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
}