0
0
GoProgramBeginner · 2 min read

Go Program to Find Smallest Element in Slice

In Go, you can find the smallest element in a slice by initializing a variable with the first element and then using a for loop with if to compare and update it, like: min := slice[0]; for _, v := range slice { if v < min { min = v } }.
📋

Examples

Input[3, 1, 4, 1, 5]
Output1
Input[10, 20, 30, 40]
Output10
Input[-5, -10, 0, 5]
Output-10
🧠

How to Think About It

To find the smallest number in a slice, start by assuming the first number is the smallest. Then, check each number one by one. If you find a number smaller than your current smallest, update your smallest number. At the end, the smallest number you have is the answer.
📐

Algorithm

1
Check if the slice is empty; if yes, handle accordingly.
2
Set the first element of the slice as the smallest number.
3
Loop through each element in the slice.
4
Compare the current element with the smallest number.
5
If the current element is smaller, update the smallest number.
6
After the loop ends, return the smallest number.
💻

Code

go
package main

import "fmt"

func main() {
    nums := []int{3, 1, 4, 1, 5}
    if len(nums) == 0 {
        fmt.Println("Slice is empty")
        return
    }
    min := nums[0]
    for _, v := range nums {
        if v < min {
            min = v
        }
    }
    fmt.Println(min)
}
Output
1
🔍

Dry Run

Let's trace the slice [3, 1, 4, 1, 5] through the code

1

Initialize min

min = 3 (first element)

2

Compare with 3

3 < 3? No, min stays 3

3

Compare with 1

1 < 3? Yes, min updated to 1

4

Compare with 4

4 < 1? No, min stays 1

5

Compare with 1

1 < 1? No, min stays 1

6

Compare with 5

5 < 1? No, min stays 1

7

Return min

min = 1

IterationCurrent Valuemin
133
211
341
411
551
💡

Why This Works

Step 1: Start with first element

We assume the first element is the smallest to have a starting point for comparison.

Step 2: Loop through slice

We check each element to see if it is smaller than the current smallest.

Step 3: Update smallest value

If a smaller element is found, we update our smallest value to that element.

🔄

Alternative Approaches

Using sort package
go
package main

import (
    "fmt"
    "sort"
)

func main() {
    nums := []int{3, 1, 4, 1, 5}
    if len(nums) == 0 {
        fmt.Println("Slice is empty")
        return
    }
    sort.Ints(nums)
    fmt.Println(nums[0])
}
This sorts the slice first, then picks the first element as smallest. It is simpler but slower for large slices because sorting is more expensive than a single pass.
Using recursion
go
package main

import "fmt"

func findMin(nums []int) int {
    if len(nums) == 1 {
        return nums[0]
    }
    minRest := findMin(nums[1:])
    if nums[0] < minRest {
        return nums[0]
    }
    return minRest
}

func main() {
    nums := []int{3, 1, 4, 1, 5}
    if len(nums) == 0 {
        fmt.Println("Slice is empty")
        return
    }
    fmt.Println(findMin(nums))
}
This uses recursion to find the smallest element. It is elegant but uses more memory due to function calls.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so it runs in linear time proportional to the slice size.

Space Complexity

It uses a fixed amount of extra space regardless of input size, so space complexity is constant.

Which Approach is Fastest?

The single loop approach is fastest because it only scans once. Sorting is slower (O(n log n)) and recursion uses extra memory.

ApproachTimeSpaceBest For
Single loopO(n)O(1)Fastest and simplest for finding smallest
SortingO(n log n)O(1)When you also need sorted data
RecursionO(n)O(n)Elegant but uses more memory
💡
Always check if the slice is empty before finding the smallest element to avoid runtime errors.
⚠️
Beginners often forget to initialize the smallest value with the first element, causing incorrect comparisons.