How to find Minimum of an array or slice in Golang Example

In these examples, You will learn how to find the minimum/smallest element of an array or slice from input from the user in Go Language.

To understand the below programs, You have the understanding the following features in Go Language.

The algorithm for this program is as follows.

-At first, the starting element of an array is assumed as the smallest element.

  • Iterate elements in Array or Slice using a range
  • Each iterated element compare with initialized first smallest number.
  • if elements are smaller than other elements, It will be a new smaller element.
  • This sequence of steps continues execution until all elements are iterated.

How to find a minimum number in a golang array or slice

This program explains the following sequence of steps.

  • Array or slice is declared and initialized with values
  • Declared smallestNumber initialized with array first element assumed as the smallest number
  • Iterated array or slice using for loop with range to calculate minimum element
  • Inside for loop for each element, if an element is smallestNumber, assign smallestNumber with an element
  • repeat the loop until all elements iteration is completed.
package main
import "fmt"

func main() {

    array: = [] int {
            11, 9, 1, 45, 411
        }
        // First element as smallest number assumption
    smallestNumber: = array[0]
        // Iterate array using rage loop
    for _,
    element: = range array {
        // check for the smallest number
        if element < smallestNumber {
            smallestNumber = element

        }
    }
    fmt.Println("Smallest number of Array/slice is  ", smallestNumber)
}

Output:

The smallest number of Array/slices is   1

How to check a minimum of array/slice elements read user input from the console golang

Instead of array elements being fixed like above, This program read the array size and elements read from the input console by a user using the Scanln function.

The logic is the same as the above program to calculate the smallest of an array or slice of numbers.

package main

import "fmt"

func main() {
    size: = 0
        // Read Input from the command line console
    fmt.Print("Number of elements n=")
    fmt.Scanln( & size)
    fmt.Println("Enter of array elements")
    // Create a dynamic array
    elements: = make([] int, size)
        // add elements to the array
    for i: = 0;i < size;i++{
        fmt.Scanln( & elements[i])
    }
    fmt.Println("Entered Array of elements:", elements)
    smallestNumber: = elements[0]
    for _,
    element: = range elements {
        if element < smallestNumber {
            smallestNumber = element

        }
    }
    fmt.Println("Smallest number of Array/slice is  ", smallestNumber)
}

The output of the above code is as follows.

Number of elements n=5  
Enter of array elements  
2  
5  
0  
1  
8  
Entered Array of elements: [2 5 0 1 8]  
The smallest number of Array/slices is 0