Easy csharp

Count occurrences of element

Problem

Count how many times 7 appears in { 3, 7, 7, 2, 7, 1 }.

Hints
  • Increment a counter when arr[i] equals target.

Your solution

TestStatusDetails
Ready — edit the code above and click Run or Submit.

Solution

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.

Explanation

Linear scan. LINQ: arr.Count(x => x == target).

Discussion

0

Sign in to join the discussion.

No discussions yet — ask the first question!

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details