DEV Community

vinay
vinay

Posted on

LinearSearch Golang

#go
package main

import (
    "fmt"
    "strconv"
)

func LinearSearch(a []int, k int) string {
    i := 0
    for i < len(a) {
        if a[i] == k {
            return "Found" + " index is " + strconv.Itoa(i)
        }
        i += 1
    }
    return "Not found index"

}

func main() {
    a := []int{10, 20, 30, 40, 50}
    fmt.Println(LinearSearch(a, 50))

}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)