In this blog, we will create a c# program that takes input from the user and check-in predefine array to check how many times that number occurs in that array.
  1. using System;
  2. using System.Linq;
  3. namespace CheckOccurrenceOfNumberInArray
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int[] array = { 1, 20, 15, 23, 15, 14, 26, 188,175,12, 36, 78, 15, 3, 5, 91, 822, 100, 1202, 78, 95, 12, 123, 12, 14, 25, 36, 15, 36, 95, 45, 10, 20, 30, 04, 50, 40, 60, 70, 80, 90, 104 };
  10. Console.WriteLine("Your Array...");
  11. for (int i = 0; i < array.Length; i++)
  12. {
  13. Console.Write(array[i] + " ");
  14. }
  15. Console.WriteLine("\n\n");
  16. Console.Write("Enter number to check occurrence : ");
  17. int number = Convert.ToInt32(Console.ReadLine());
  18. int occurrences = array.Count(x => x == number);
  19. Console.WriteLine("Occurrence of {0} in given array is {1}", number, occurrences);
  20. Console.ReadKey();
  21. }
  22. }
  23. }

Explanation

  • In this program, first we declare an array of integer types and then print it to show the user.
  • Then take input from the user.
  • Check that input with our array using the LINQ count method and pass expression in that method also that element is equal to the given number.
Output 1
C# Program For Check Total Occurrence Of A Number In An Array
Output 2
C# Program For Check Total Occurrence Of A Number In An Array
Output 3
C# Program For Check Total Occurrence Of A Number In An Array