0
0
DSA Goprogramming~30 mins

Search in 2D Matrix in DSA Go - Build from Scratch

Choose your learning style9 modes available
Search in 2D Matrix
📖 Scenario: You are working with a grid-like map represented as a 2D matrix. Each cell contains a number. You want to find if a specific number exists in this map.
🎯 Goal: Build a program that searches for a given number in a 2D matrix and prints true if found, otherwise false.
📋 What You'll Learn
Create a 2D slice of integers called matrix with exact values
Create an integer variable called target to hold the number to search
Use nested for loops with variables i and j to search the matrix
Print true if target is found, else print false
💡 Why This Matters
🌍 Real World
Searching in 2D grids is common in games, maps, and image processing where data is arranged in rows and columns.
💼 Career
Understanding how to traverse and search 2D data structures is essential for software developers working with matrices, tables, or grid-based data.
Progress0 / 4 steps
1
Create the 2D matrix
Create a 2D slice of integers called matrix with these exact rows and values: { {1, 3, 5}, {7, 9, 11}, {13, 15, 17} }
DSA Go
Hint

Use a slice of slices to create the 2D matrix with the exact numbers.

2
Set the target number
Create an integer variable called target and set it to 9
DSA Go
Hint

Just create a variable named target and assign the number 9.

3
Search the matrix for the target
Use nested for loops with variables i and j to check each element in matrix. Create a boolean variable found set to false initially. If matrix[i][j] equals target, set found to true and break both loops.
DSA Go
Hint

Use two loops to go through rows and columns. Use found to remember if you found the target.

4
Print the search result
Print the value of the boolean variable found using fmt.Println(found)
DSA Go
Hint

Use fmt.Println(found) to show if the target was found.