Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Occurrence Of Array Element in C#

 

Occurrence Of Array Element in C#


In this Article we will see the Occurrence Of Array Element in C#

C# code

  public static void OccurenceOfArrayElement()

        {

            int[] arr = new int[] { 1, 4, 2, 5, 4, 2, 5, 1, 6, 2, 5, 3, 8, 9, 6 };

 

            int[] occurence = new int[arr.Length];

            int visit = -1;

 

            for (int i = 0; i < arr.Length; i++)

            {

                int count = 1;

                for (int j = i + 1; j < arr.Length; j++)

                {

                    if (arr[i] == arr[j])

                    {

                        count++;

                        //To avoid counting same element again 

                        occurence[j] = visit;

                    }

                }

                if (occurence[i] != visit)

                    occurence[i] = count;

            }

 

            //Displays the frequency of each element present in array 

 

            for (int i = 0; i < occurence.Length; i++)

            {

                if (occurence[i] != visit)

                    Console.WriteLine("  element-  " + arr[i] + " occurence is   " + occurence[i]);

            }

            Console.ReadLine();

        }


Output



Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now