yjp 5 місяців тому
батько
коміт
c7f27a6280
1 змінених файлів з 27 додано та 0 видалено
  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
+}