Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MostFrequent
- {
- static void Main()
- {
- Console.WriteLine("Enter size of the array: ");
- int n = int.Parse(Console.ReadLine());
- int[] array = new int[n];
- Console.WriteLine("Enter the elements of the array: ");
- for (int i = 0; i < n; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- int freq=0;
- int maxFreq=0;
- int maxNum=0;
- for (int i=0; i<array.Length-1; i++)
- {
- for (int j=i+1; j<array.Length; j++)
- {
- if (array[i]==array[j])
- {
- freq++;
- }
- }
- if (freq > maxFreq)
- {
- maxFreq = freq;
- maxNum = array[i];
- freq = 0;
- }
- else
- {
- freq = 0;
- }
- if (maxFreq>array.Length/2)
- {
- break;
- }
- }
- Console.WriteLine("{0} -> {1} times", maxNum, maxFreq+1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement