File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,7 @@ func main() {
118118 })
119119
120120 fmt.Println (si)
121+
121122 // [11, 13, 13]
122123}
123124```
Original file line number Diff line number Diff line change 1+ package slices
2+
3+ import (
4+ "cmp"
5+ "slices"
6+ )
7+
8+ // Insert inserts an element into a sorted slice.
9+ func Insert [S ~ []E , E cmp.Ordered ](s S , e E , unique bool ) S {
10+ i , found := slices .BinarySearch (s , e )
11+ if found && unique {
12+ return s
13+ }
14+ return slices .Insert (s , i , e )
15+ }
16+
17+ // Delete deletes an element from a sorted slice.
18+ func Delete [S ~ []E , E cmp.Ordered ](s S , e E ) S {
19+ i , found := slices .BinarySearch (s , e )
20+ if found {
21+ return slices .Delete (s , i , i + 1 )
22+ }
23+
24+ return s
25+ }
26+
27+ // Contains checks if a slice contains an element.
28+ func Contains [T cmp.Ordered ](s []T , e T ) bool {
29+ _ , found := slices .BinarySearch (s , e )
30+
31+ return found
32+ }
You can’t perform that action at this time.
0 commit comments