Maximum occurrence of number in an integer Array using C#
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculationApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter number");
int num = int.Parse(Console.ReadLine());
int[] Arr = new int[num];
Console.WriteLine("Enter " + num + "number");
for (int i = 0; i < num; i++)
{
Arr[i] = int.Parse(Console.ReadLine());
}
int[] result = new int[2];
result = Calculation.MaxOccurance(Arr);
Console.Write("Number:- " + result[0] + " Occurance:- " + result[1]);
Console.ReadLine();
}
}
}
Calculation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculationApp
{
public static class Calculation
{
//Method for Calculating the maximum occurrence of number in array.
public static int[] MaxOccurance(int[] Arr)
{
int Number = 0;
int MaxOccurance = 0;
int Search = 0;
for (int i = 0; i < Arr.Length; i++)
{
int count = 0;
Search = Arr[i];
for (int j = 0; j < Arr.Length; j++)
{
if (Arr[i] == Arr[j])
{
count++;
}
}
if (count > MaxOccurance)
{
MaxOccurance = count;
Number = Arr[i];
}
}
int[] result = new int[2];
result[0] = Number;
result[1] = MaxOccurance;
return result;
}
}
}
No comments:
Post a Comment