0
0
GoProgramBeginner · 2 min read

Go Program to Find Largest Element in Slice

In Go, you can find the largest number in a slice by looping through it and keeping track of the maximum value using code like: max := slice[0]; for _, v := range slice { if v > max { max = v } }.
📋

Examples

Input[3, 5, 1, 9, 2]
Output9
Input[10, 10, 10]
Output10
Input[-5, -1, -10]
Output-1
🧠

How to Think About It

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

Algorithm

1
Check if the slice is empty; if yes, handle accordingly.
2
Set the first element as the current largest number.
3
Go through each number in the slice.
4
Compare the current number with the largest number.
5
If the current number is bigger, update the largest number.
6
After checking all numbers, return the largest number.
💻

Code

go
package main

import "fmt"

func main() {
    nums := []int{3, 5, 1, 9, 2}
    max := nums[0]
    for _, v := range nums {
        if v > max {
            max = v
        }
    }
    fmt.Println(max)
}
Output
9
🔍

Dry Run

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

1

Initialize max

max = 3 (first element)

2

Compare 3 with max

3 is not greater than 3, max stays 3

3

Compare 5 with max

5 is greater than 3, max updated to 5

4

Compare 1 with max

1 is not greater than 5, max stays 5

5

Compare 9 with max

9 is greater than 5, max updated to 9

6

Compare 2 with max

2 is not greater than 9, max stays 9

7

Return max

max = 9

IterationCurrent ValueMax Value
133
255
315
499
529
💡

Why This Works

Step 1: Start with first element

We assume the first number is the largest to have a starting point for comparison.

Step 2: Compare each element

Each number is checked against the current largest number using if v > max.

Step 3: Update largest number

If a bigger number is found, we update max to that number to keep track of the largest.

🔄

Alternative Approaches

Using sort package
go
package main

import (
    "fmt"
    "sort"
)

func main() {
    nums := []int{3, 5, 1, 9, 2}
    sort.Ints(nums)
    fmt.Println(nums[len(nums)-1])
}
This sorts the slice first, then picks the last element as the largest. It is simpler but slower for large slices because sorting is more expensive than a single pass.
Using a function to find max
go
package main

import "fmt"

func maxInSlice(nums []int) int {
    max := nums[0]
    for _, v := range nums {
        if v > max {
            max = v
        }
    }
    return max
}

func main() {
    fmt.Println(maxInSlice([]int{3, 5, 1, 9, 2}))
}
This approach wraps the logic in a reusable function, improving code organization and reuse.

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

Time Complexity

The program checks each element once, so the time grows linearly with the slice size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The single pass loop is fastest because it only scans once. Sorting is slower (O(n log n)) but simpler to write.

ApproachTimeSpaceBest For
Single pass loopO(n)O(1)Fastest for large slices
Sorting sliceO(n log n)O(1)Simple code, small slices
Function wrapperO(n)O(1)Reusable and clean code
💡
Always check if the slice is empty before finding the largest to avoid runtime errors.
⚠️
Beginners often forget to initialize the max variable with the first element, causing incorrect results or errors.