Advertisement
soxa

MostFrequentNum

Dec 16th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System;
  2. // Write a program that finds the most frequent number in an array. Example:
  3. //  {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} --> 4 (5 times)
  4.  
  5. class MostFrequentNumber
  6. {
  7.     static void Main()
  8.     {
  9.         //Input
  10.         int[] array = { 03, -03, 0, 00, -0, -00 };
  11.         int loop = (array.Length - 1);
  12.         int counter = 1;
  13.         int maxCounter = 0;
  14.         int maxElement = 0;
  15.  
  16.         //Solution
  17.         Array.Sort(array);
  18.  
  19.         for (int i = 0; i < loop; i++)
  20.         {
  21.             if (array[i] == array[i + 1])
  22.             {
  23.                 counter++;
  24.                 if (counter > maxCounter)
  25.                 {
  26.                     maxCounter = counter;
  27.                     maxElement = array[i];
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 counter = 1;
  33.             }
  34.         }
  35.  
  36.         //Output
  37.         Console.WriteLine("{0} --> ({1} times)",maxElement,maxCounter);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement