C# | Find Total Occurrence of a number in an Array

Learn, how to find total number of occurrence of a given number?
[Last updated : March 19, 2023]

Total occurrences of a number in an array

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.

C# program to find total occurrence of a given number in an array

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

C# Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.