Golang Program to remove given element from the array


In this tutorial, we will learn how to remove an element from an array using simple for loop approach. The logic behind this approach is that create a new array and the particular index element which is to be removed don’t add it to the new array. Let’s have a look how to execute it with the use of different examples.

Remove Element from the Array in main Function

In this method, we will execute the entire program inside main function. An original array and a new array will be created to execute the removal of elements from the array. Let’s have a look and inculcate the method to solve this problem.

Algorithm

Step 1 − Declare main package and import fmt package in the program.

Step 2 − Create a function main and further create an array inside the function and fill the array with values from which one has to deleted.

Step 3 − Create a variable i and assign the index to the variable which is to be deleted.

Step 4 − check a condition that if the index is less than 0 or if the value is greater than the length of array then print that the index is out of bounds.

Step 5 − If the condition is not satisfied then, create an array named originalarr and variable j, further run a loop till the range of array and check when the variable i value is not equal to the index value, assign the old array value to the new array and do j++.

Step 6 − When the value is similar, this means that index is to be deleted, according to the algorithm that index value will not be added in the array and the loop continues.

Step 7 − After the loop is terminated, we reslice the array to remove extra index and print the new array on console using fmt.Println() function.

Example

Golang program to remove an element from the array using main approach

package main
import ( 
   "fmt"
)
func main() {

   originalarr := [5]string{"Vatsal", "Kartik", "Ritika", "Veronica", "Isha"} //create array
   fmt.Println("The original array is:", originalarr)
   
   i := 3 //index to be deleted
   fmt.Println("The index to be deleted is:", i)
   if i < 0 || i >= len(originalarr) {
      fmt.Println("The given index is out of bounds.")
   } else {
      j := 0
      for index := range originalarr { //run a loop till end of array
      if i != index {
            originalarr[j] = originalarr[index] //assign the old array values to new array
            j++
         }
      }
      
      newarr := originalarr[:j] //reslicing array to remove extra index
      fmt.Println("The new array after deleting the element is:", newarr)
   }
}

Output

The original array is: [Vatsal Kartik Ritika Veronica Isha]
The index to be deleted is: 3
The new array after deleting the element is: [Vatsal Kartik Ritika Isha]

Remove Element from Array with User-Defined Function

In this method,we will execute the entire program inside external function. An original array and a new array will be created to execute the removal of elements from the array. The original array will be passed as an argument from the main function. Let’s have a look how to solve this problem.

Algorithm

Step 1 − Declare main package and import fmt package in the program.

Step 2 − Create a function named remove_ele which contains the array as a parameter and further create a variable inside the function and assign the index of element to be deleted to the variable.

Step 3 − check a condition that if the index is less than 0 or if the value is greater than the length of array then print that the index is out of bounds.

Step 4 − If the condition is not satisfied then, create an array named originalarr and variable j, further run a loop till the range of array and check when the variable i value is not equal to the index value, assign the old array value to the new array and do j++.

Step 5 − When the value is similar, this means that index is to be deleted, according to the algorithm that index value will not be added in the array and the loop continues.

Step 6 − After the loop is terminated, we reslice the array to remove extra index and print the new array on console using fmt.Println() function.

Step 7 − The main function calls the external function with array as an argument after which the whole program runs.

Example

Golang program to remove an element from the array using external function approach

package main
import (
   "fmt"
)

func remove_ele(originalarr []int) {

   i := 3 //index to be deleted
   fmt.Println("The index to be deleted is:", i)
   if i < 0 || i >= len(originalarr) {
      fmt.Println("The given index is out of bounds.")
   } else {
   
      j := 0
      for index := range originalarr { 
      //run a loop till end of array
      
         if i != index {
            originalarr[j] = originalarr[index] 
            //assign old array value to new array
            j++
         }
      }
      
      newarr := originalarr[:j] 
      //reslicing array to remove extra index
      fmt.Println("The new array after deleting the element is:", newarr)
   }
   
}
func main() {
   arr := []int{10, 20, 30, 40, 50} 
   //create array
   fmt.Println("The array is:", arr)
   remove_ele(arr)
}

Output

The array is: [10 20 30 40 50]
The index to be deleted is: 3
The new array after deleting the element is: [10 20 30 50]

Conclusion

We executed the program of removing an element from an array using two methods. In the first method, we used the main approach to delete the element and in the second approach we used the external approach in which the array is passed as an argument through main function. Similar outputs are obtained in both cases. Hence, program executed successfully.

Updated on: 13-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements