mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Merge branch 'main' of github.com:duke-git/lancet
This commit is contained in:
@@ -1094,7 +1094,7 @@ func IndexOf[T comparable](arr []T, val T) int {
|
|||||||
// LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
|
// LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
|
||||||
// Play: https://go.dev/play/p/DokM4cf1IKH
|
// Play: https://go.dev/play/p/DokM4cf1IKH
|
||||||
func LastIndexOf[T comparable](slice []T, item T) int {
|
func LastIndexOf[T comparable](slice []T, item T) int {
|
||||||
for i := len(slice) - 1; i > 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
if item == slice[i] {
|
if item == slice[i] {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -873,8 +873,9 @@ func TestIndexOf(t *testing.T) {
|
|||||||
func TestLastIndexOf(t *testing.T) {
|
func TestLastIndexOf(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestLastIndexOf")
|
assert := internal.NewAssert(t, "TestLastIndexOf")
|
||||||
|
|
||||||
arr := []string{"a", "a", "b", "c"}
|
arr := []string{"a", "b", "b", "c"}
|
||||||
assert.Equal(1, LastIndexOf(arr, "a"))
|
assert.Equal(0, LastIndexOf(arr, "a"))
|
||||||
|
assert.Equal(2, LastIndexOf(arr, "b"))
|
||||||
assert.Equal(-1, LastIndexOf(arr, "d"))
|
assert.Equal(-1, LastIndexOf(arr, "d"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user