Advertisement
Naralex

Arrays-MostFrequentNumber

Jan 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2.  
  3. class MostFrequentNumber
  4. {
  5.     static void Main()
  6.     {
  7.         /*09.Write a program that finds the most frequent number in an array.
  8.         Example:{4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3}  4 (5 times)*/
  9.  
  10.         Console.Write("Array size: ");
  11.         int arraySize = int.Parse(Console.ReadLine());
  12.         int[] arr = new int[arraySize];
  13.  
  14.         int number;    
  15.  
  16.         for (int index = 0; index < arraySize; index++)
  17.         {
  18.             Console.Write("Position {0} : ", index + 1);
  19.             number = int.Parse(Console.ReadLine());
  20.             arr[index] = number;
  21.         }
  22.  
  23.         int[] arr2 = new int[arraySize];
  24.         int count=0,maxCount=0;
  25.  
  26.         for (int i = 0; i < arr.Length; i++)
  27.         {
  28.             count = 0;
  29.  
  30.             for (int k = 0; k < arr.Length; k++)
  31.             {
  32.                 if (arr[i] == arr[k])
  33.                 {
  34.                     count++;
  35.                     arr2[i] +=1 ;
  36.                 }
  37.                 if (count > maxCount)
  38.                 {
  39.                     maxCount = count;
  40.                 }
  41.             }
  42.         }
  43.         for (int i = 0; i < arr2.Length; i++)
  44.         {
  45.             if (arr2[i] == maxCount)
  46.             {
  47.                 Console.WriteLine("{0} ({1} times)", arr[i], maxCount);
  48.                 break;
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement