Advertisement
Hristo_B

Arrays.MostFrequentNumber

Jun 30th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2.  
  3. class MostFrequentNumber
  4. {
  5.     static void Main()
  6.     {
  7.         //Write a program that finds the most frequent number in an array. Example:
  8.         //{4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3}  4 (5 times)
  9.  
  10.         int[] array = { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 1, 1, 1, 9, 3 };
  11.         Console.WriteLine("{0}", string.Join(", ", array));
  12.  
  13.         Console.WriteLine();
  14.  
  15.         int maxSequence = 1;
  16.         int tempSequence = 1;
  17.         int placeholder = 0;
  18.  
  19.         for (int i = 0; i < array.Length; i++)
  20.         {
  21.             for (int j = 1; j < array.Length; j++)
  22.             {
  23.                 if (array[i] ==  array[j])
  24.                 {
  25.                     tempSequence++;
  26.                 }
  27.             }
  28.             if (maxSequence < tempSequence)
  29.             {
  30.                 maxSequence = tempSequence;
  31.                 placeholder = i;
  32.                
  33.             }
  34.             tempSequence = 0;
  35.         }
  36.         Console.WriteLine("Number {0} is repeated {1} times in the array!", array[placeholder], maxSequence);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement