0
0
DSA Goprogramming~30 mins

Count Occurrences of Element in Sorted Array in DSA Go - Build from Scratch

Choose your learning style9 modes available
Count Occurrences of Element in Sorted Array
📖 Scenario: You have a sorted list of numbers representing daily temperatures recorded in a city. You want to find out how many times a specific temperature was recorded.
🎯 Goal: Build a Go program that counts how many times a given number appears in a sorted array.
📋 What You'll Learn
Create a sorted array of integers called temperatures with exact values
Create a variable called target to hold the temperature to count
Write a loop to count how many times target appears in temperatures
Print the count of occurrences
💡 Why This Matters
🌍 Real World
Counting occurrences of values in sorted data is common in statistics, data analysis, and reporting.
💼 Career
This skill helps in roles like data analyst, software developer, and quality assurance where data processing is needed.
Progress0 / 4 steps
1
Create the sorted array of temperatures
Create a sorted array of integers called temperatures with these exact values: 12, 15, 15, 15, 18, 20, 20, 22
DSA Go
Hint

Use a slice literal to create temperatures with the exact numbers in order.

2
Set the target temperature to count
Create an integer variable called target and set it to 15
DSA Go
Hint

Use target := 15 to create the variable.

3
Count how many times the target appears
Write a for loop using the variable temp to iterate over temperatures and count how many times target appears. Store the count in a variable called count
DSA Go
Hint

Start count at 0. Use for _, temp := range temperatures to loop. Increase count when temp == target.

4
Print the count of occurrences
Write a print statement to display the value of count
DSA Go
Hint

Use fmt.Println(count) to print the number of times target appears.