Q:

Find total number of occurrence of a given number using C# program

belongs to collection: C# Basic Programs | array programs

0

Given array of integers and we have to find occurrence of a give number.

For example we have list of integers: 10 23 10 24 10

Here we will take a counter initialized with 0. Then take input from console. And check given number to each number of list if item matched. Then we will increase the counter.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i       = 0 ;
            int count   = 0 ;
            int item    = 0 ;

            int[] arr1   = new int[5];
                        
            //Read numbers into array
            Console.WriteLine("Enter numbers : ");
            for (i = 0; i < 5; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr1[i] = int.Parse(Console.ReadLine());
            }

            Console.Write("Enter item : ");
            item = int.Parse(Console.ReadLine());

            for (i = 0; i < 5; i++)
            {
                if (item == arr1[i])
                {
                    count++;
                }
            }

            Console.WriteLine("Total occurrence of item "+item + " is : "+count);
            
            Console.WriteLine();
        }
    }
}

Output

Enter numbers :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 10
Element[5]: 50
Enter item : 10
Total occurrence of item 10 is : 2

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Merge two arrays into third array using C# program... >>
<< Delete given element from array using C# program...