Parcourir la source

添加slice函数

yjp il y a 5 mois
Parent
commit
c7f27a6280
1 fichiers modifiés avec 27 ajouts et 0 suppressions
  1. 27 0
      slice/slice.go

+ 27 - 0
slice/slice.go

@@ -45,3 +45,30 @@ func ExtractValue[T any, R any](s []T, addTo func(v T) (R, bool)) []R {
 
 	return retSlice
 }
+
+func ExtractIntersectionValue[T comparable](aList, bList []T) []T {
+	aList = RemoveRepeatElement(aList)
+	bList = RemoveRepeatElement(bList)
+	cList := make([]T, 0)
+
+	tmpMap := make(map[T]int)
+	for _, item := range aList {
+		tmpMap[item] = 0
+	}
+
+	for _, item := range bList {
+		if _, ok := tmpMap[item]; ok {
+			cList = append(cList, item)
+		}
+	}
+
+	return cList
+}
+
+func ExtractUnionValue[T comparable](aList, bList []T) []T {
+	cList := make([]T, 0)
+	cList = append(cList, aList...)
+	cList = append(cList, bList...)
+	cList = RemoveRepeatElement(cList)
+	return cList
+}