From 9c4feeca87b519d332257a650b5cf3e33f1d2109 Mon Sep 17 00:00:00 2001 From: Richie Varghese Date: Sun, 20 Mar 2022 00:58:41 +0530 Subject: [PATCH] added remove elem and removeat --- array/array.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/array/array.go b/array/array.go index 2687e4e..490a139 100644 --- a/array/array.go +++ b/array/array.go @@ -54,8 +54,8 @@ func Filter[T arrTypes](a []T, f func(T) bool) []T { return b } -// RemoveAt removes the first element in the array that equals the searchKey. -func RemoveAt[T arrTypes](a []T, searchKey T) []T { +// RemoveElem removes the first element in the array that equals the searchKey. +func RemoveElem[T arrTypes](a []T, searchKey T) []T { _, i, _ := in(a, searchKey) // check if i is out of bounds if i+1 > len(a) { @@ -64,6 +64,14 @@ func RemoveAt[T arrTypes](a []T, searchKey T) []T { return append(a[:i], a[i+1:]...) } +// RemoveAt removes the element at the index i. +func RemoveAt[T arrTypes](a []T, i int) []T { + if i+1 > len(a) { + return a[:i] + } + return append(a[:i], a[i+1:]...) +} + // InsertAt inserts the newValue at the index i. func InsertAt[T arrTypes](a []T, i int, v T) []T { // check if i is out of bounds