+GetUniqueElementsOfStringSlices
This commit is contained in:
		
							
								
								
									
										20
									
								
								comparisons.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								comparisons.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								comparisons_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								comparisons_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestGetUniqueElementsOfStringSlices(t *testing.T) {
 | 
			
		||||
	data := []map[string][]string{
 | 
			
		||||
		{
 | 
			
		||||
			"a":              []string{"apple", "banana", "cherry"},
 | 
			
		||||
			"b":              []string{"banana", "cherry", "date", "fig"},
 | 
			
		||||
			"expectedResult": []string{"apple"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			"a":              []string{"banana", "cherry", "date", "fig"},
 | 
			
		||||
			"b":              []string{"apple", "banana", "cherry"},
 | 
			
		||||
			"expectedResult": []string{"date", "fig"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			"a":              []string{"apple", "banana", "cherry"},
 | 
			
		||||
			"b":              []string{"apple", "banana", "cherry"},
 | 
			
		||||
			"expectedResult": []string{},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			"a":              []string{},
 | 
			
		||||
			"b":              []string{"apple", "banana", "cherry"},
 | 
			
		||||
			"expectedResult": []string{},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			"a":              []string{"apple", "banana", "cherry"},
 | 
			
		||||
			"b":              []string{},
 | 
			
		||||
			"expectedResult": []string{"apple", "banana", "cherry"},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, test := range data {
 | 
			
		||||
		result := GetUniqueElementsOfStringSlices(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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user