+ GetSharedElementsOfStringSlices
This commit is contained in:
parent
0b60553c59
commit
7e057ed6c0
@ -1,5 +1,24 @@
|
|||||||
package utils
|
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
|
// GetUniqueElementsOfStringSlices finds the elements that are in the first slice but not in the second slice
|
||||||
func GetUniqueElementsOfStringSlices(a, b []string) []string {
|
func GetUniqueElementsOfStringSlices(a, b []string) []string {
|
||||||
// Create a map to store the elements of "b"
|
// Create a map to store the elements of "b"
|
||||||
|
@ -5,6 +5,52 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestGetSharedElementsOfStringSlices(t *testing.T) {
|
||||||
|
data := []map[string][]string{
|
||||||
|
{
|
||||||
|
"a": []string{"apple", "banana", "cherry"},
|
||||||
|
"b": []string{"banana", "cherry", "date", "fig"},
|
||||||
|
"expectedResult": []string{"banana", "cherry"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a": []string{"banana", "cherry", "date", "fig"},
|
||||||
|
"b": []string{"apple", "banana", "cherry"},
|
||||||
|
"expectedResult": []string{"banana", "cherry"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a": []string{"apple", "banana", "cherry"},
|
||||||
|
"b": []string{"apple", "banana", "cherry"},
|
||||||
|
"expectedResult": []string{"apple", "banana", "cherry"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a": []string{},
|
||||||
|
"b": []string{"apple", "banana", "cherry"},
|
||||||
|
"expectedResult": []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a": []string{"apple", "banana", "cherry"},
|
||||||
|
"b": []string{},
|
||||||
|
"expectedResult": []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a": []string{},
|
||||||
|
"b": []string{},
|
||||||
|
"expectedResult": []string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range data {
|
||||||
|
result := GetSharedElementsOfStringSlices(test["a"], test["b"])
|
||||||
|
if !reflect.DeepEqual(result, test["expectedResult"]) {
|
||||||
|
if len(result) == 0 && len(test["expectedResult"]) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, test["expectedResult"], test)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetUniqueElementsOfStringSlices(t *testing.T) {
|
func TestGetUniqueElementsOfStringSlices(t *testing.T) {
|
||||||
data := []map[string][]string{
|
data := []map[string][]string{
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user