Count number of occurrences(or frequency) in a sorted array in C#

 

Count number of occurrences(or frequency) in a sorted array in C#


C# code

        static void Main(string[] args)

        {

            //   Count number of occurrences(or frequency) in a sorted array

            int[] arr = { 1, 2, 2, 2, 2, 3, 4, 7, 8, 8 };

            int n1 = arr.Length;

            Console.Write("Enter the Traget Number : ");

 

            int number = int.Parse(Console.ReadLine());

 

            Console.Write(countOccurrences(arr, n1, number));

 

 

            Console.ReadLine();

        }

        static int countOccurrences(int[] arr, int n, int x)

        {

            int res = 0;

 

            for (int i = 0; i < n; i++)

                if (x == arr[i])

                    res++;

 

            return res;

        }


output



Share this

Related Posts

Previous
Next Post »