MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Linear

Function Linear

search/linear.go:4–11  ·  view source on GitHub ↗

Linear Simple linear search algorithm that iterates over all elements of an array in the worst case scenario

(array []int, query int)

Source from the content-addressed store, hash-verified

2
3// Linear Simple linear search algorithm that iterates over all elements of an array in the worst case scenario
4func Linear(array []int, query int) (int, error) {
5 for i, item := range array {
6 if item == query {
7 return i, nil
8 }
9 }
10 return -1, ErrNotFound
11}

Callers 2

TestLinearFunction · 0.85
BenchmarkLinearFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestLinearFunction · 0.68
BenchmarkLinearFunction · 0.68