Golang Program To Remove All Occurrences Of An Element In An Array


In this tutorial, we will see to write a go language program to remove all occurrences of an element in an array. Removing occurrences from an array means that we wish to remove an entry completely from the array.

Remove All Occurances Of An Element In An Array Using External Function

The following code illustrates how we can remove all the occurrences of an element in an array by using a user-defined 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 1

package main
import "fmt"
func removeOccurrence(nums [10]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 := [10]int{1, 2, 3, 4, 4, 5, 4, 7, 8, 4}
   fmt.Println("The first array Arr1 is:", arr1)
   fmt.Println()
   output := removeOccurrence(arr1, 4)
   fmt.Println("The array obtained after removing all the occurrences of element 4 are:", output)
}

Output

The first array Arr1 is: [1 2 3 4 4 5 4 7 8 4]

The array obtained after removing all the occurrences of element 4 are: [1 2 3 5 7 8]

Algorithm

Step 1 − Import the fmt package.

Step 2 − Defining a function named removeOccurrences().

Step 3 − This function uses for loop to iterate over the array and a if condition to check if the current element of the array Is equal to the element that we wish to remove.

Step 4 − If the condition is true then we need to append the array by removing the value. Repeat the process till whole array is iterated upon.

Step 5 − Return the resultant array.

Step 6 − Start the main() funciton.

Step 7 − Initialize an array and assign values to it further print the array on the screen using.

Step 8 − Call the removeOccurrences() function by passing the array and the value that we wish to remove as arguments to it.

Step 9 − Store the value returned by the function in a separate variable and print it on the screen using fmt.Println() function.

Example 1

Let us now look at another example of removing all the occurrences of an element from an array in golang.

package main
import "fmt"
func removeOccurrence(nums []int, val int) []int {
   var i int
   for {
      if i == len(nums) {
         break
      }
      if nums[i] == val {
         nums = nums[:i+copy(nums[i:], nums[i+1:])]
         i = 0
      }
      i++
   }
   return nums
}
func main() {
   arr1 := []int{1, 2, 3, 4, 4, 5, 4, 7, 8, 4}
   fmt.Println("The first array Arr1 is:", arr1)
   fmt.Println()
   output := removeOccurrence(arr1, 4)
   fmt.Println("The array obtained after removing all the occurrences of element 4 are:", output)
}

Output

The first array Arr1 is: [1 2 3 4 4 5 4 7 8 4]

The array obtained after removing all the occurrences of element 4 are: [1 2 3 5 7 8]

Example 2

Let us now write a go language code to remove the string elements from an array.

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]

Conclusion

We have successfully compiled and executed a go language program to remove all the occurrences of an element from an array along with examples.

Updated on: 02-Jan-2023

416 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements