Kaynağa Gözat

添加slice函数

yjp 5 ay önce
ebeveyn
işleme
c7f27a6280
1 değiştirilmiş dosya ile 27 ekleme ve 0 silme
  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
+}