Skip to content

Commit a649cdb

Browse files
Merge pull request #1 from theycallmemac/patch-1
Create LinearSearch.go
2 parents 9672917 + 522fce4 commit a649cdb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

LinearSearch.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func linearSearch(array []int, query int) int {
6+
for i, item := range array {
7+
if item == query {
8+
return i
9+
}
10+
}
11+
return -1
12+
}
13+
14+
func main() {
15+
16+
fmt.Println("Linear search:")
17+
array := []int{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
18+
index := linearSearch(array, 10)
19+
if index == -1 {
20+
fmt.Println("Number not found")
21+
} else {
22+
fmt.Println("Index: ", index)
23+
fmt.Println("array[", index, "] = ", array[index])
24+
}
25+
}

0 commit comments

Comments
 (0)