Advertisement
FLISEN

Untitled

Jan 11th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2.  
  3. class MostFrequent
  4. {
  5.     static void Main()
  6.     {
  7.  
  8.         Console.WriteLine("Enter size of the array: ");
  9.         int n = int.Parse(Console.ReadLine());
  10.         int[] array = new int[n];
  11.  
  12.         Console.WriteLine("Enter the elements of the array: ");
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             array[i] = int.Parse(Console.ReadLine());
  16.         }
  17.  
  18.         int freq=0;
  19.         int maxFreq=0;
  20.         int maxNum=0;
  21.  
  22.         for (int i=0; i<array.Length-1; i++)
  23.         {
  24.             freq = 0;
  25.             for (int j=i+1; j<array.Length; j++)
  26.             {
  27.                 if (array[i]==array[j])
  28.                 {
  29.                     freq++;
  30.                 }
  31.             }
  32.  
  33.             if (freq > maxFreq)
  34.             {
  35.                 maxFreq = freq;
  36.                 maxNum = array[i];
  37.             }
  38.            
  39.             if (maxFreq>array.Length/2)
  40.             {
  41.                 break;
  42.             }
  43.         }
  44.  
  45.         Console.WriteLine("{0} -> {1} times", maxNum, maxFreq+1);
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement