Golang Program to delete an item from the array without using the library function


An array in go language is defined as the data structure that is used to store elements in contiguous memory locations. Arrays allow us to search and store elements at constant time. arrays use index to store elements which starts from 0 and goes to n – 1, where n is the length of the array.

Method 1: Using An External User-Defined Function

The following method, illustrates how we can remove a particular integer element from the array of integers using an external function.

Algorithm

  • Step 1 − Import the fmt package.

  • Step 2 − Defining a function named removeOccurrence().

  • Step 3 − In this function we are checking if the current element of array is equal to the value that we wish to delete or not.

  • Step 4 − If not then we are storing that element in the same array otherwise ignore the element and repeat the process till whole of the array is iterated.

  • Step 5 − Once every element is checked we need to return the final array as the result.

  • Step 6 − Start the main() function. Initialize an array and assign values to it.

  • Step 7 − Print the array on the screen.

  • Step 8 − Call the removeOccurrence() function by passing the array and element to be removed as arguments to it.

  • Step 9 − Store the result obtained from the function in a variable called output.

  • Step 10 − Print the final result on the screen using fmt.Println() function.

Example

In the following code, we have used a user-defined function externally to remove an item from the array of integers

package main
import "fmt"
func removeOccurrence(nums [8]int, val int) []int {
   lenArr := len(nums)
   var k int = 0
   for i := 0; i < lenArr; {
      if nums[i] != val {
         nums[k] = nums[i]
         k++
      }
      i++
   }
   return nums[0:k]
}

func main() {
   arr1 := [8]int{11, 21, 32, 41, 54, 79, 83, 47}
   fmt.Println("The first array Arr1 is:", arr1)
   fmt.Println()
   output := removeOccurrence(arr1, 41)
   fmt.Println("The array obtained after removing the element 41 is:", output)
}

Output

The first array Arr1 is: [11 21 32 41 54 79 83 47]

The array obtained after removing the element 41 is: [11 21 32 54 79 83 47]

Method 2: Using A Single Function

In this method, we are going to see two different techniques of deleting an item from an array using 2 examples of go programming.

Algorithm

  • Step 1 − Import the fmt package.

  • Step 2 − Start the main() function. Initialize an array of strings and assign values to it.

  • Step 3 − Print the array on the screen.

  • Step 4 − Store the value to be removed in a new variable and use for loop along with the range function to iterate over the array.

  • Step 5 − If the current element is not equal to the value to be removed then update that value to the array and continue the loop until the whole array is iterated upon.

  • Step 6 − Now, copy the new array thus obtained into a new array and print the final array thus obtained on the screen using fmt.Println() function.

Example

package main
import (
   "fmt"
)
func main() {
   originalArray := [5]string{"a", "b", "c", "c", "d"}
   fmt.Println("The original array is:", originalArray)
   val := "c"
   j := 0
   for _, v := range originalArray {
      if v != val {
         originalArray[j] = v
         j++
      }
   }
   newArray := originalArray[:j]
   fmt.Println("The new array is:", newArray)
}

Output

The original array is: [a b c c d]
The new array is: [a b d]

Example 2

package main
import "fmt"
func main() {
   arr := []int{11, 21, 32, 41, 54, 79, 83, 47}
   fmt.Println("The given array is:", arr)
   fmt.Println()
   var val int = 32
   var j int = 0
   fmt.Println("The value to be removed is:", val)
   var res []int
   for i := 0; i < len(arr); i++ {
      if arr[i] != val {
         arr[j] = arr[i]
         j++
      }
   }
   res = arr[:j]
   fmt.Println("The array obtained after removing the element", val, "is:", res)
}

Output

The given array is: [11 21 32 41 54 79 83 47]

The value to be removed is: 32
The array obtained after removing the element 32 is: [11 21 41 54 79 83 47]

Conclusion

We have successfully compiled and executed a go language program to delete a particular element from the array along with examples. We have used three examples in this article, the first program uses an external function to remove an integer value from the array of integers while the second program implies the logic over the array of strings. In the last example, we have shown how we can use for loop to remove elements from the arrays.

Updated on: 16-Feb-2023

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements