repl.it
@talha131/

Correct Go: Iterate Over An Array And Remove Elements

Go

https://www.oncrashreboot.com/iterate-over-an-array-and-remove-elements

fork
loading
Files
  • main.go
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import "fmt"

func remove(slice []int, s int) []int {
	return append(slice[:s], slice[s+1:]...)
}

func main() {
	eg := []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}

	l := len(eg) - 1
	for l >= 0 {
    if eg[l] > 99 {
			eg = remove(eg, l)
		}
    l = l - 1
	}

	fmt.Println(eg)
}