Advertisement
FLISEN

Untitled

Jan 11th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 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.             for (int j=i+1; j<array.Length; j++)
  25.             {
  26.                 if (array[i]==array[j])
  27.                 {
  28.                     freq++;
  29.                 }
  30.             }
  31.             if (freq > maxFreq)
  32.             {
  33.                 maxFreq = freq;
  34.                 maxNum = array[i];
  35.                 freq = 0;
  36.             }
  37.             else
  38.             {
  39.                 freq = 0;
  40.             }
  41.             if (maxFreq>array.Length/2)
  42.             {
  43.                 break;
  44.             }
  45.         }
  46.  
  47.         Console.WriteLine("{0} -> {1} times", maxNum, maxFreq+1);
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement