fix: The LastIndexOf method can never return the index of the first element (#83)

Co-authored-by: JiangCheng <jiangcheng@kezaihui.com>
This commit is contained in:
MicklsandJiangCheng authored and GitHub committed 2023-03-28 20:02:57 +08:00
1 parent f09e521783
commit 3b497532f3
2 files changed
+6 -5

No files matched your search

+1 -1
View File
@@ -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.
// Play: https://go.dev/play/p/DokM4cf1IKH
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] {
return i
}