ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

F# Array Minimum Finder

F#

Goal -- WPM

Ready
Exercise Algorithm Area
1(*
2Finds the minimum element in an array of integers.
3Assumes the array contains only integers.
4*)
5let findMin (arr: int array) : int option =
6// Handle edge case: empty array has no minimum
7if arr.Length = 0 then
8None
9else
10// Initialize minVal with the first element of the array
11let mutable minVal = arr.[0]
12
13// Iterate through the array starting from the second element
14// The loop boundary is arr.Length - 1, so we check up to the last element.
15for i in 1 .. arr.Length - 1 do
16// If the current element is smaller than the current minimum,
17// update minVal.
18if arr.[i] < minVal then
19minVal <- arr.[i]
20
21// Return the minimum value wrapped in an option
22Some minVal
23
24
25// Example Usage:
26let numbers1 = [| 5; 2; 8; 1; 9 |]
27let numbers2 = [| 10 |]
28let numbers3 = [| -3; -1; -7; -2 |]
29let emptyArray : int array = [||]
30
31match findMin numbers1 with
32| Some min -> printfn "Minimum of [| 5; 2; 8; 1; 9 |] is: %d" min
33| None -> printfn "Array is empty."
34
35match findMin numbers2 with
36| Some min -> printfn "Minimum of [| 10 |] is: %d" min
37| None -> printfn "Array is empty."
38
39match findMin numbers3 with
40| Some min -> printfn "Minimum of [| -3; -1; -7; -2 |] is: %d" min
41| None -> printfn "Array is empty."
42
43match findMin emptyArray with
44| Some min -> printfn "Minimum of empty array is: %d" min
45| None -> printfn "Minimum of empty array is: Array is empty."
Algorithm description viewbox

F# Array Minimum Finder

Algorithm description:

This F# function efficiently finds the smallest integer within an array. It iterates through the array, keeping track of the smallest value encountered so far. This is a fundamental operation used in various data processing tasks, such as finding the lowest price in a list of products or the minimum temperature recorded over a period.

Algorithm explanation:

The `findMin` function takes an array of integers and returns an `int option`. It first checks if the array is empty. If it is, it returns `None` because there's no minimum element. Otherwise, it initializes a mutable variable `minVal` with the first element of the array. It then iterates through the rest of the array (from the second element to the last) using a `for` loop with explicit bounds. Inside the loop, it compares the current element with `minVal`. If the current element is smaller, `minVal` is updated. After the loop finishes, `minVal` holds the smallest value in the array, which is then returned wrapped in `Some`. The time complexity is O(N), where N is the number of elements in the array, as each element is visited exactly once. The space complexity is O(1) because only a constant amount of extra space is used for variables like `minVal` and the loop counter.

Pseudocode:

function findMin(array):
  if array is empty:
    return None

  min_value = array[0]

  for i from 1 to array.length - 1:
    if array[i] < min_value:
      min_value = array[i]

  return Some(min_value)