TO COUNT OCCURENCE OF ELEMENT ( C#)

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//WAP IN C SHARP TO COUNT THE TOTAL OCCURENCE OF ELEMENT AND DISPLAY THEM ALL.

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

namespace occurance
{
class Program
{
static void Main(string[] args)
{
int n, k, count;
int[] a = new int[50];
int[] b = new int[50];
Console.WriteLine(“Enter range”);
n = int.Parse(Console.ReadLine());
Console.WriteLine(“Enter the elements”);
for (int i = 0; i < n; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
b[0] = a[0];
k = 1;
for (int i = 1; i < n; i++)
{
count = 0;
for (int j = 0; j < n; j++)
{
if (a[i] == b[j])
{
count++;
}
}
if (count == 0)
{
b[k] = a[i];
k++;
}
}
int p;
for (int j = 0; j < k; j++)
{
p = 0;
for (int i = 0; i < n; i++)
{
if (b[j] == a[i])
{
p++;
}
}
Console.WriteLine(“\n” + b[j] + “occurs” + p + “times”);
}
Console.ReadLine();

}
}
}

…………………………output……………………………………………
Enter range
5
Enter the elements
1
4
4
5
6

1occurs1times

4occurs2times

5occurs1times

6occurs1times

Leave a Reply

Your email address will not be published. Required fields are marked *