Skip to content

Instantly share code, notes, and snippets.

@pyk
Created April 12, 2014 04:52
Show Gist options
  • 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
Copy link

ghost commented Oct 6, 2014

A solution from me

package main

import (
    "fmt"
)

func main() {
    x := []int{
        48, 96, 86, 68,
        57, 82, 63, 70,
        37, 34, 83, 27,
        19, 97, 9, 17,
    }

    slice := []bool{}

    var smallest int

    for i := 0; i < len(x); {

        for _, value := range x {

            if x[i] == value {

                continue
            }

            switch true {

            case x[i] < value:
                slice = append(slice, true)
            default:
                continue

            }

        }

        if len(slice) == (len(x) - 1) {

            smallest = x[i]
            break
        }

        slice = []bool{}

        i++

    }

    fmt.Println("The smallest number is ", smallest) // output:  The smallest number is  9

}

@RobMokkink
Copy link

package main

import "fmt"
import "sort"


func main() {

    x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
    }

    
    sort.Ints(x)
    lowest := x[0]
    fmt.Println(lowest)                                                                                                                
}

@myklll
Copy link

myklll commented Jan 10, 2019

package main

import (
	"fmt"
)

func main() {
	x := []int{
		48, 96, 86, 68,
		57, 82, 63, 70,
		37, 34, 83, 27,
		19, 97, 9, 17,
	}
	max := x[0]
	min := x[0]
	for _, v := range x {
		if max < v {
			max = v
		}
		if min > v {
			min = v
		}
	}
	fmt.Println(min, max)
}

@byansanur
Copy link

`package main

import "fmt"

func main() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
y := x[0]

for _, v := range x {
	if v < y {
		y = v
	}
}
fmt.Println("The smallest value is : ", y)

}`

@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