Golang program to remove an element from a slice


In this tutorial, we will learn how to remove an element from a slice using variety of examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Let’s go through the example to understand things.

Syntax

func copy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a slice and using append function add values in the slice.

  • Step 3 − Print the slice on the console to actually know about the original slice.

  • Step 4 − Call the function remove_ele from the main function with slice and the index to be removed as parameters.

  • Step 5 − In the function remove_ele first of all check that whether the index is out of bounds or not.

  • Step 6 − If the index is out of bounds print the same statement on console but if its not out of bounds use a built-in copy function to shift the elements after the index by one position.

  • Step 7 − Then reslice the slice to remove the extra index which is generated while using copy function.

  • Step 8 − Return the slice one less than its size so that the extra element can be ignored.

  • Step 9 − The output will be printed using fmt.println() function where ln refers to next line.

  • Step 10 − Here, it’s an important point to note that the element is not actually deleted from the memory.

  • Step11 − It is shifted to fill the gap in order to fulfil the problem’s purpose.

  • Step 12 − The slice here is resliced again in order to remove the extra element and the changes being done are also reflected in the original slice.

Example

In this method, we will remove an element from a slice using built-in copy function. Here the element will not be deleted from the memory. In its place new values will be copied to actually execute the desired operation. The output value will be printed using fmt.Println() function which is the print statement in Golang.

package main
import "fmt"
func main() {

   // Create a slice of integers
   var Slice []int
   Slice = append(Slice, 10) // create slice using append function
   Slice = append(Slice, 20)
   Slice = append(Slice, 30)
   Slice = append(Slice, 40)
   Slice = append(Slice, 50)

   fmt.Println("The slice before removing element is:", Slice) 

   Slice = remove_ele(Slice, 1)
   fmt.Println("The index of element to be removed is:", 1)

   fmt.Println("The slice after removing element is: ")
   fmt.Println(Slice)
}

func remove_ele(Slice []int, index int) []int {
   // check whether the index is out of bounds or not
   if index < 0 || index >= len(Slice) {
      fmt.Println("The index is out of bounds")
   }

   copy(Slice[index:], Slice[index+1:])

   // Reslice the slice to remove the last element
   return Slice[:len(Slice)-1]
}

Output

The slice before removing element is: [10 20 30 40 50]
The index of element to be removed is: 1
The slice after removing element is: 
[10 30 40 50]

Conclusion

We executed the program of searching the elements of a slice using one example. In this particular example, we used a custom function to remove an element from the slice with the help of copy function. Both the examples give similar output. Hence the program executed successfully.

Updated on: 17-Jan-2023

719 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements