Advertisement
Krissy

09. MostFrequent

Jan 6th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2.  
  3. class FindMostFrequent
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Please enter the number of elements in your array:");
  8.         int sizeOfArray = int.Parse(Console.ReadLine());
  9.         int[] Array = new int[sizeOfArray];
  10.         int[] frequencyOfElements = new int[sizeOfArray];
  11.  
  12.  
  13.         for (int i = 0; i < sizeOfArray; i++)
  14.         {
  15.             Console.WriteLine("Please enter the elements for the array:");
  16.             Array[i] = int.Parse(Console.ReadLine());
  17.         }
  18.  
  19.         int counter = 0;
  20.  
  21.         while (counter < sizeOfArray)
  22.         {
  23.             for (int j = counter; j < sizeOfArray; j++)
  24.             {
  25.                 if (Array[j] == Array[counter])
  26.                 {
  27.                     frequencyOfElements[j]++;
  28.                 }
  29.             }
  30.             counter++;
  31.         }
  32.         int maxFrequency = 1;
  33.         int positionMaxFrequency = 0;
  34.         for (int i = 0; i < sizeOfArray; i++)
  35.         {
  36.             if (frequencyOfElements[i] > maxFrequency)
  37.             {
  38.                 maxFrequency = frequencyOfElements[i];
  39.                 positionMaxFrequency = i;
  40.             }
  41.         }
  42.         if (maxFrequency==1)
  43.         {
  44.             Console.WriteLine("All elements are different.");
  45.         }
  46.         else
  47.         {
  48.             Console.WriteLine("The most frequent number is {0}, ({1} times)",
  49.             Array[positionMaxFrequency], maxFrequency);
  50.         }      
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement