Golang Program To Get The Last Given Number Of Items From The Array


In this tutorial, we will write a go language program to remove the last given number of items from an array. We can do this by either using the inbuilt functions in go or using 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 Last given Number of Items from an Array of Integers using Append()

In this method, we will write a go language program to remove the last given number of items from an array by using the append() library function. This code runs in linear time i.e, the time complexity of this method is O(n).

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 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 − 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 make() function and append values to the array. Further, print the array on the screen.

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

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

Step 7 − Print the final array on the screen.

Example

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

package main
import "fmt"
   
// function to remove the last element from the array
func delLastElem(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
      
   // getting element at index
   elem := array[index]
   result := delLastElem(array, index)
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements after ", elem, "is:\n", result)
}

Output

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

Method 2: GoLang Program to Remove the Last given Elements from an Array in Main Function

In this method, we will write a go language program to remove the last 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 after 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 last given elements from an array

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 = 2
   elem := array[index]
   result := array[:index]
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after deleting elements after", elem, "is:\n", result)
}

Output

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

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

In this method, we will use for loop to remove the values after a particular index from the last in go programming language. Since we are using for loops so the time complexity of this method will be O(n2).

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 after which the elements should be deleted.

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

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

Example

Golang program to remove the last 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 last 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 after", 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 after 44 is:
[11 20 13]

Conclusion

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

Updated on: 10-Jan-2023

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements