- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete Elements in a Slice in Golang
Slices in Golang are dynamically-sized sequences that provide a more powerful interface than arrays. They are commonly used for storing collections of related data. Sometimes, we may want to delete elements from a slice. In this article, we will discuss how to delete elements in a slice in Golang.
Deleting Elements in a Slice
In Golang, we can delete elements from a slice using the built-in append() function. Here's how it works −
Example
Here's an example of how to use the deleteElement() function to delete an element from a slice −
package main import "fmt" func deleteElement(slice []int, index int) []int { return append(slice[:index], slice[index+1:]...) } func main() { slice := []int{1, 2, 3, 4, 5} fmt.Println("Before deletion:", slice) // Delete element at index 2 slice = deleteElement(slice, 2) fmt.Println("After deletion:", slice) }
Output
Before deletion: [1 2 3 4 5] After deletion: [1 2 4 5]
In this example, we define a function deleteElement() that takes a slice slice and an index index as input. The function then returns a new slice that contains all the elements of the slice except the one at the specified index.
The append() function takes two or more slices as arguments and concatenates them into a single slice. In this case, we're creating a new slice by concatenating the elements of slice before the specified index with the elements after the index.
Example
In this example, we create a slice of integers slice and specify the index of the element we want to delete. We then call the deleteElement() function and pass in the slice and index as arguments. The function returns a new slice with the specified element removed, which we assign back to slice.
package main import "fmt" func main() { slice := []int{1, 2, 3, 4, 5} index := 2 // index of element to delete // Delete element at index 2 slice = deleteElement(slice, index) fmt.Println(slice) // Output: [1 2 4 5] } func deleteElement(slice []int, index int) []int { return append(slice[:index], slice[index+1:]...) }
Output
[1 2 4 5]
Deleting Multiple Elements
We can also delete multiple elements from a slice by calling the deleteElement() function multiple times. However, this can be inefficient if we need to delete a large number of elements. In this case, we can use the append() function with the ... operator to concatenate multiple slices.
Example
Here's an example of how to delete multiple elements from a slice using the append() function −
package main import ( "fmt" "sort" ) func deleteElements(slice []int, indices []int) []int { // Sort indices in descending order sort.Sort(sort.Reverse(sort.IntSlice(indices))) for _, index := range indices { slice = append(slice[:index], slice[index+1:]...) } return slice } func main() { slice := []int{1, 2, 3, 4, 5} indices := []int{1, 3} // indices of elements to delete // Delete elements at indices 1 and 3 slice = deleteElements(slice, indices) fmt.Println(slice) // Output: [1 3 5] }
Output
[1 3 5]
In this example, we define a function deleteElements() that takes a slice and a slice of indices as input. The function first sorts the indices in descending order using the sort.Sort() function. This is necessary because deleting elements from a slice changes the indices of the remaining elements.
The function then loops over the indices in indices, calling the deleteElement() function with each index to delete the corresponding element from the slice. The resulting slice is returned by the function.
Example
Here's an example of how to use the deleteElements() function to delete multiple elements from a slice −
package main import ( "fmt" "sort" ) func deleteElements(slice []int, indices []int) []int { // Sort indices in descending order sort.Sort(sort.Reverse(sort.IntSlice(indices))) for _, index := range indices { slice = append(slice[:index], slice[index+1:]...) } return slice } func main() { slice := []int{1, 2, 3, 4, 5} indices := []int{1, 3} // indices of elements to delete // Delete elements at indices 1 and 3 slice = deleteElements(slice, indices) fmt.Println(slice) // Output: [1 3 5] }
Output
[1 3 5]
In this example, we create a slice of integers slice and a slice of booleans boolSlice. We then remove elements at index 1 and 2 using the append() function and print the new slices.
Example
package main import "fmt" func main() { // create a slice of integers slice := []int{1, 2, 3, 4, 5} // remove elements at index 1 and 2 slice = append(slice[:1], slice[3:]...) // create a slice of booleans boolSlice := []bool{true, false, true, true} // remove the element at index 2 boolSlice = append(boolSlice[:2], boolSlice[3:]...) // print the new slices fmt.Println(slice) // Output: [1 4 5] fmt.Println(boolSlice) // Output: [true false true] }
Output
[1 4 5] [true false true]
In the first example, we remove the elements at index 1 and 2 using append() and the slice expression slice[:1] and slice[3:]. The ... operator after slice[3:] is used to spread the elements in the slice. This means that instead of appending a slice to slice[:1], we append each individual element of slice[3:] to slice[:1]. This effectively removes the elements at index 1 and 2.
In the second example, we remove the element at index 2 using a similar approach. We use boolSlice[:2] to include the first two elements of the slice and boolSlice[3:] to include all the elements from index 3 to the end of the slice. The ... operator is used again to spread the elements in the slice.
It's important to note that the append() function creates a new slice with the updated elements. This means that we need to assign the result of append() back to the original slice variable. If we don't do this, we'll end up with the original slice and no elements removed.
Conclusion
Deleting elements from a slice in Golang is simple and can be done using the append() function and slice expressions. By using these techniques, we can efficiently remove elements from a slice without needing to create a new slice from scratch.