- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to find the first occurrence of a specific element in array using linear search
In this Golang article, we will find the first occurrence of a specific element in array using linear search by using recursion and iterative method.
Linear search is a simple search algorithm that checks every element in a list or array one by one, starting from the beginning, until a target value is found or the entire list has been searched. It is also known as sequential search.
Syntax
func firstOcc(arr []int, target int) int {…}
The firstOcc() function is used to find the first occurrence of a specific element in array using linear search iteratively. It takes an integer array, target element as arguments to it.
func firstOcc(arr []int, target int, index int) int {…}
The firstOcc() function is used to find the first occurrence of a specific element in array using linear search recursively. It takes an integer array, target element and current index of the array as arguments to it.
Algorithm
Step 1 − First, we need to import the fmt package.
Step 2 − Now, create a firstOcc() function that finds the first occurrence of a specific element in an array using linear search.
Step 3 − It takes an array and a target element as input and returns the index of the resultant first occurrence of the target element in the array.
Step 4 − The function returns -1, if the target element is not found in the array.
Step 5 − It uses a simple linear search algorithm to traverse the array from starting and compares each element with the target element. And if it is found, it will return the index of that element.
Step 6 − Start the main() function. Inside the main() function, create an array with some elements.
Step 7 − Define the target element.
Step 8 − Now, call the firstOcc() function and pass the array & target element as arguments to the function.
Step 9 − Further, the resultant message with the first occurrence of the target element with index is printed on the screen by using the fmt.Printf() function.
Example 1
In this example, we will define a firstOcc() function using iterative method that is used to find the first occurence of a specific element in array using linear search.
package main import "fmt" func firstOcc(arr []int, target int) int { for i, v := range arr { if v == target { return i } } return -1 } func main() { arr := []int{4, 8, 3, 2, 7, 4, 5, 9, 10} target := 9 index := firstOcc(arr, target) if index == -1 { fmt.Printf("First Occurrence of element %d not found in array\n", target) } else { fmt.Printf("First Occurrence of element %d found at index %d\n", target, index) } }
Output
First Occurrence of element 9 found at index 7
Example 2
In this example, we will define a firstOcc() function using recursive method that is used to find the first occurrence of a specific element in array using linear search.
package main import "fmt" func firstOcc(arr []int, target int, index int) int { if index >= len(arr) { return -1 } if arr[index] == target { return index } return firstOcc(arr, target, index+1) } func main() { arr := []int{49, 23, 33, 14, 56, 46, 17, 28, 69, 10} target := 56 index := firstOcc(arr, target, 0) if index == -1 { fmt.Printf("First Occurrence of element %d not found in array\n", target) } else { fmt.Printf("First Occurrence of element %d found at index %d\n", target, index) } }
Output
First Occurrence of element 56 found at index 4
Conclusion
We have successfully compiled and executed a go language program to find the first occurrence of a specific element in an array using linear search by using recursion and iterative methodalong with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.