Skip to content

Instantly share code, notes, and snippets.

@pyk
Created April 12, 2014 04:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyk/10519339 to your computer and use it in GitHub Desktop.
Save pyk/10519339 to your computer and use it in GitHub Desktop.
find the biggest and smallest number in the given slice or array using go lang
package main
import "fmt"
func main() {
var n, smallest, biggest int
x := []int{
48,96,86,68,
57,82,63,70,
37,34,83,27,
19,97, 9,17,
}
for _,v:=range x {
if v>n {
fmt.Println(v,">",n)
n = v
biggest = n
} else {
fmt.Println(v,"<",n)
}
}
fmt.Println("The biggest number is ", biggest)
for _,v:=range x {
if v>n {
fmt.Println(v,">",n)
} else {
fmt.Println(v,"<",n)
n = v
smallest = n
}
}
fmt.Println("The smallest number is ", smallest)
}
48 > 0
96 > 48
86 < 96
68 < 96
57 < 96
82 < 96
63 < 96
70 < 96
37 < 96
34 < 96
83 < 96
27 < 96
19 < 96
97 > 96
9 < 97
17 < 97
The biggest number is 97
48 < 97
96 > 48
86 > 48
68 > 48
57 > 48
82 > 48
63 > 48
70 > 48
37 < 48
34 < 37
83 > 34
27 < 34
19 < 27
97 > 19
9 < 19
17 > 9
The smallest number is 9
@margach
Copy link

margach commented Dec 14, 2021

Pass in the Slice and it will return the biggest number in the slice.

func Biggest(seq []int) int {
	var cache int

	for i := 0; i < len(seq)-1; i++ {
		if seq[i] > seq[i+1] {
			if seq[i] > cache {
				cache = seq[i]
			}
		}

	}
	return cache
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment