Count how many times 7 appears in { 3, 7, 7, 2, 7, 1 }.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
int[] arr = { 3, 7, 7, 2, 7, 1 };
int target = 7;
int count = 0;
foreach (var x in arr)
if (x == target) count++;
Console.WriteLine(count);
}
}
Try solving on your own first, then reveal the official answer.
Linear scan. LINQ: arr.Count(x => x == target).