0
0
DSA Goprogramming~30 mins

Linear Search Algorithm in DSA Go - Build from Scratch

Choose your learning style9 modes available
Linear Search Algorithm
📖 Scenario: You have a list of product IDs in a store inventory. You want to find if a specific product ID is available in the list.
🎯 Goal: Build a simple program that uses linear search to find the position of a product ID in the list.
📋 What You'll Learn
Create a slice of integers called products with the exact values: 101, 203, 405, 302, 101, 509
Create an integer variable called target with the value 302
Write a for loop using the index variable i to iterate over products
Inside the loop, use an if statement to check if products[i] equals target
If found, print the message "Product found at index: " followed by the index i
If not found after the loop, print "Product not found"
💡 Why This Matters
🌍 Real World
Searching for items in a list is common in inventory management, contact lists, or any collection of data.
💼 Career
Understanding linear search helps in debugging and optimizing simple search tasks before moving to advanced algorithms.
Progress0 / 4 steps
1
Create the product list
Create a slice of integers called products with these exact values: 101, 203, 405, 302, 101, 509
DSA Go
Hint

Use products := []int{} to create the slice with the given numbers.

2
Set the target product ID
Create an integer variable called target and set it to 302
DSA Go
Hint

Use target := 302 to create the variable.

3
Write the linear search loop
Write a for loop with index variable i to iterate over products. Inside the loop, use an if statement to check if products[i] equals target. If yes, print "Product found at index: " and the index i, then return to stop the search.
DSA Go
Hint

Use a for loop from 0 to len(products). Inside, check if products[i] equals target. If yes, print and return.

4
Print not found message
After the loop, print "Product not found" if the product was not found.
DSA Go
Hint

Print "Product not found" after the loop if no match was found.