diff --git a/README.md b/README.md index b0719aa..257f349 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Go library for PHP community with convenient functions * [ArrayUdiff](#user-content-arrayudiff) * [ArraySum](#user-content-arraysum) * [ArrayIntersect](#user-content-arrayintersect) + * [ArrayMin/ArrayMax](#user-content-arrayminarraymax) * [Range](#user-content-range) * [EqualSlices](#user-content-equalslices) * [Collections](#user-content-collections) @@ -399,6 +400,17 @@ pgo.ArrayIntersect([]int{12, 54, 32, 12, 33}, []int{3, 12, 54, 9}, []int{12, 33, pgo.ArrayIntersect([]string{"foo", "bar", "baz", "fizz", "bazz", "fizz", "fizz"}, []string{"bar", "fizz"}, []string{"foo", "bar", "hey"}) // []string{"foo", "bar", "fizz"} ``` +#### ArrayMin/ArrayMax + +finds minimum/maximum value from []T + +```go +res = pgo.ArrayMax([]int{3, 1, 2, 9}) // res == 9 +res = pgo.ArrayMax([]float64{-3.12, -1.678, -2.01, -9.007}) // res == -1.678 +res = pgo.ArrayMin([]int{3, 1, 2, 9}) // res == 1 +res = pgo.ArrayMin([]float64{3.2, 1.0837, 2.123, 9.87}) // res == 1.0837 +``` + #### Range creates an int slice of min to max range diff --git a/array.go b/array.go index 016801a..ce4744f 100644 --- a/array.go +++ b/array.go @@ -289,3 +289,35 @@ func EqualSlices[T comparable](a, b []T) bool { return true } + +// ArrayMax finds maximum value from []T +func ArrayMax[T constraints.IntFloat](a []T) T { + var max T + if len(a) > 0 { + max = a[0] + } + + for _, v := range a { + if max < v { + max = v + } + } + + return max +} + +// ArrayMin finds minimum value from []T +func ArrayMin[T constraints.IntFloat](a []T) T { + var min T + if len(a) > 0 { + min = a[0] + } + + for _, v := range a { + if min > v { + min = v + } + } + + return min +} diff --git a/array_test.go b/array_test.go index 3c35841..819c259 100644 --- a/array_test.go +++ b/array_test.go @@ -418,3 +418,55 @@ func TestEqual(t *testing.T) { } } } + +func TestArrayMax(t *testing.T) { + res := pgo.ArrayMax([]int{}) + assert.Equal(t, 0, res) + + res = pgo.ArrayMax([]int{3, 1, 2, 9}) + assert.Equal(t, 9, res) + + res = pgo.ArrayMax([]int{-3, -1, -2, -9}) + assert.Equal(t, -1, res) + + res = pgo.ArrayMax([]int{-3, -1, -2, -9, 3, 1, 2, 9}) + assert.Equal(t, 9, res) + + resF := pgo.ArrayMax([]float64{}) + assert.Equal(t, float64(0), resF) + + resF = pgo.ArrayMax([]float64{3.2, 1.0837, 2.123, 9.87}) + assert.Equal(t, 9.87, resF) + + resF = pgo.ArrayMax([]float64{-3.12, -1.678, -2.01, -9.007}) + assert.Equal(t, -1.678, resF) + + resF = pgo.ArrayMax([]float64{3.2, 1.0837, 2.123, 9.87, -3.12, -1.678, -2.01, -9.007}) + assert.Equal(t, 9.87, resF) +} + +func TestArrayMin(t *testing.T) { + res := pgo.ArrayMin([]int{}) + assert.Equal(t, 0, res) + + res = pgo.ArrayMin([]int{3, 1, 2, 9}) + assert.Equal(t, 1, res) + + res = pgo.ArrayMin([]int{-3, -1, -2, -9}) + assert.Equal(t, -9, res) + + res = pgo.ArrayMin([]int{-3, -1, -2, -9, 3, 1, 2, 9}) + assert.Equal(t, -9, res) + + resF := pgo.ArrayMin([]float64{}) + assert.Equal(t, float64(0), resF) + + resF = pgo.ArrayMin([]float64{3.2, 1.0837, 2.123, 9.87}) + assert.Equal(t, 1.0837, resF) + + resF = pgo.ArrayMin([]float64{-3.12, -1.678, -2.01, -9.007}) + assert.Equal(t, -9.007, resF) + + resF = pgo.ArrayMin([]float64{-3.12, -1.678, -2.01, -9.007, 3.2, 1.0837, 2.123, 9.87}) + assert.Equal(t, -9.007, resF) +}