Golang Program To Remove The First Given Number Of Items From The Array


In this tutorial, we will write a go language program to remove the first given number of items from an array. We can do this by either using the inbuilt functions in go or using the for loops. The first method is more efficient in functionality than the second one but we will discuss both these methods in this program.

Method 1: Remove the First given Number of Items from an Array of Integers using Internal Functions

In this method, we will write a go language program to remove the first given number of items from an array by using the append() library function.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size, and capacity as arguments

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

The append function is used to add values to an array slice. It takes a 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 an array containing all the values.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then, create a function to remove the elements from a given index. This function accepts the array as an argument and returns the final array.

Step 3 − Now, we need to start the main() function.

Step 4 − Here, initialize an array of integers using the make() function and append values to the array. Further, print the array on the screen.

Step 5 − Store the index in a variable before which the elements should be deleted. Store the element present at that index in a variable.

Step 6 − Now, call the delFirstElem() function by passing the array and the index as arguments to the function and storing the array obtained.

Step 7 − Print the final array on the screen.

Example

Golang program to remove the first given number of items from an array of integers using internal functions

package main
import "fmt"

// function to remove the first element from the array
func delFirstElem(array []int, index int) []int {
   return append(array[index:])
}
func main() {
   
   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 1, 2, 3, 4, 5)
   fmt.Println("The given array is:", array)
   var index int = 2
   elem := array[index]
   result := delFirstElem(array, index)
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements before", elem, "is:\n", result)
}

Output

The given array is: [1 2 3 4 5]
The provided index is: 2
Array obtained after deleting elements before 3 is: [3 4 5]

Method 2: Remove the First Given Elements from an Array using User-Defined Function

In this method, we will write a go language program to remove the first given elements from an array in the main() section of the program.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Now, we need to start the main() function.

Step 3 − Here, initialize an array of integers using make() function and append values to the array. Further, print the array on the screen by using fmt.Println() function.

Step 4 − Store the index in a variable before which the elements should be deleted. Store the element present at that index in a variable.

Step 5 − Now, to re-slice the array use the: operator and store values present before the provided index in a variable.

Step 6 − Further, print the final array on the screen.

Example

Golang program to remove the first given elements from an array using user-defined function

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 11, 20, 13, 44, 56)
   fmt.Println("The given array is:", array)
   var index int = 3
   elem := array[index]
   result := array[index:]
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements before", elem, "is:\n", result)
}

Output

The given array is: [11 20 13 44 56]
The provided index is: 3
Array obtained after deleting elements before 44 is:
[44 56]

Method 3: Remove the First given Number of Items from an Array of Integers using For Loop

In this method, we will use for loop to remove the values before a particular index from the start of an array in a go programming language.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Now, we need to start the main() function.

Step 3 − Here, initialize an array of integers using make() function and append values to the array. Further, print the array on the screen.

Step 4 − Store the index and value at that index in a variable before which the elements should be deleted.

Step 5 − Now, use for loop to iterate over the array and store the elements present in places after the index in the new array.

Step 6 − Finally, print the final array on the screen.

Example

Golang program to remove the first given number of items from an array of integers using for loop

package main
import "fmt"
func main() {
   array := make([]int, 0, 8)
   var result []int
   array = append(array, 11, 20, 13, 44, 56, 96, 54, 97)
   fmt.Println("The given array is:", array)

   // getting the index
   var index int = 3

   // re-slicing the array
   for i := 0; i < len(array); i++ {
      if i >= index {
         result = append(result, array[i])
      }
   }
   elem := array[index]
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements before", elem, "is:\n", result)
}

Output

The given array is: [11 20 13 44 56 96 54 97]
The provided index is: 3
Array obtained after deleting elements before 44 is:
[44 56 96 54 97]

Conclusion

We have successfully compiled and executed a go language program to remove the first given number of items in the array along with examples.

Updated on: 10-Jan-2023

623 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements