# How to Find the Smallest Integer in the Array in Golang

## The challenge

Given an array of integers your solution should find the smallest integer.

For example:

• Given `[34, 15, 88, 2]` your solution will return `2`
• Given `[34, -345, -1, 100]` your solution will return `-345`

You can assume, for the purpose of this challenge, that the supplied array will not be empty.

## The solution in Golang

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```package solution
func SmallestIntegerFinder(numbers []int) int {
curr := numbers[0]
for _, v := range numbers {
if v<curr {
curr = v
}
}
return curr
}
```Code language: Go (go)```

Option 2:

``````package solution
import "sort"
func SmallestIntegerFinder(numbers []int) int {
sort.Ints(numbers)
return numbers[0]
}
```Code language: Go (go)```

Option 3:

``````package solution
func SmallestIntegerFinder(numbers []int) int {
return quickSort(numbers)[0]
}
func quickSort( input []int) []int{
if len(input)==0 {
return []int{}
}
index:=len(input)/2
temp:=input[index]
var lower []int
var greater []int
for i:= range input{
if i==index {
continue
}
if input[i] <= temp {
lower = append(lower, input[i])
} else {
greater = append(greater, input[i])
}
}
result:=append(quickSort(lower), temp)
return append(result, quickSort(greater)...)
}
```Code language: Go (go)```

## Test cases to validate our solution

``````package solution_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math/rand"
"sort"
"time"
)
func _solution(numbers []int) int {
sort.Ints(numbers)
return numbers[0]
}
var _ = Describe("Test Example", func() {
It("should work for sample tests", func() {
Expect(Expect(SmallestIntegerFinder([]int{34, 15, 88, 2})).To(Equal(2)))
Expect(Expect(SmallestIntegerFinder([]int{34, -345, -1, 100})).To(Equal(-345)))
})
rand.Seed(time.Now().UTC().UnixNano())
min, max := -100, 100
It("should work for random tests", func() {
for i := 0; i < 500; i++ {
arrLen := 10 + rand.Intn(100)
var arrInts []int
for j := 0; j < arrLen; j++ {
arrInts = append(arrInts, rand.Intn(max - min + 1) + min)
}
ts := SmallestIntegerFinder(arrInts)
r := _solution(arrInts)
Expect(ts).To(Equal(r))
}
})
})
```Code language: Go (go)```

Tags:
Subscribe
Notify of