Collections
Search and Count
A loop can count how many collection items match a target.
search
A search checks each item until it finds or counts matches.
Search and Count
SearchCount.cs
Replay: real traced execution (multi-file project)
using System;
class Program
{
static void Main()
{
string target = "red";
string[] labels = { "red", "blue", "red", "gold" };
int matches = 0;
foreach (string label in labels)
{
if (label == target)
{
matches++;
}
}
Console.WriteLine($"target={target}");
Console.WriteLine($"matches={matches}");
}
}
using System;
class Program
{
static void Main()
{
string target = "blue";
string[] labels = { "red", "blue", "red", "gold" };
int matches = 0;
foreach (string label in labels)
{
if (label == target)
{
matches++;
}
}
Console.WriteLine($"target={target}");
Console.WriteLine($"matches={matches}");
}
}
using System;
class Program
{
static void Main()
{
string target = "green";
string[] labels = { "red", "blue", "red", "gold" };
int matches = 0;
foreach (string label in labels)
{
if (label == target)
{
matches++;
}
}
Console.WriteLine($"target={target}");
Console.WriteLine($"matches={matches}");
}
}
target ← red, matches ← 0
4{5 static void Main()6 {7 string target→ red = "red"; //@target="blue", "green"8 string[] labels = { "red", "blue", "red", "gold" };9 int matches→ 0 = 0;foreach (string label in labels)
pass 1 of 411foreach (string labelred in labels)12{13 if (label == target)All 4 passes — pass 1 is the card above pass labeltargetmatches1 red red 0 → 1 2 blue — — 3 red red 1 → 2 4 gold — — matches ← 1
pass 1 of 212{13 if (labelred == targetred)14 {15 matches→ 1++;16 }matches ← 2
pass 2 of 212{13 if (labelred == targetred)14 {15 matches→ 2++;16 }Console.WriteLine($"target={target}");
19 Console.WriteLine($"target={targetred}");20 Console.WriteLine($"matches={matches2}");21}outputtarget=red matches=2
target ← blue, matches ← 0
4{5 static void Main()6 {7 string target→ blue = "blue";8 string[] labels = { "red", "blue", "red", "gold" };9 int matches→ 0 = 0;foreach (string label in labels)
pass 1 of 411foreach (string labelred in labels)12{13 if (label == target)All 4 passes — pass 1 is the card above pass labeltargetmatches1 red — — 2 blue blue 0 → 1 3 red — — 4 gold — — matches ← 1
12{13 if (labelblue == targetblue)14 {15 matches→ 1++;16 }Console.WriteLine($"target={target}");
19 Console.WriteLine($"target={targetblue}");20 Console.WriteLine($"matches={matches1}");21}outputtarget=blue matches=1
target ← green, matches ← 0
4{5 static void Main()6 {7 string target→ green = "green";8 string[] labels = { "red", "blue", "red", "gold" };9 int matches→ 0 = 0;foreach (string label in labels)
pass 1 of 411foreach (string labelred in labels)12{13 if (label == target)All 4 passes — pass 1 is the card above pass label1 red 2 blue 3 red 4 gold Console.WriteLine($"target={target}");
19 Console.WriteLine($"target={targetgreen}");20 Console.WriteLine($"matches={matches0}");21}outputtarget=green matches=0
Count Matches
- The target is
red. - The loop checks each label in order.
- When a label equals the target,
matchesincreases by one. - Non-matching labels leave the count unchanged.
| Label | Matches
red? | Count after check | | --- | --- | --- | |red| yes |1| |blue| no |1| |red| yes |2| |gold| no |2|
Exercise: SearchCount.cs
Loop through labels, count how many match a target, and print the final match count