Venciity

[Jumpstart C#] Arrays 07-Repeated

Feb 18th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. /* Напишете програма която отпечатва най-често срещаното число в масив. Числата в масива са в границите от 0 до 1023.
  2.    Пример: { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 } -> 4 (5 times)  */
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _07_Repeated
  10. {
  11.     class Repeated
  12.     {
  13.         static void Main()
  14.         {
  15.             //Console.Write("Numbers of array = ");
  16.             //int n = Int32.Parse(Console.ReadLine());
  17.             //int[] array = new int[n];
  18.             //for (int i = 0; i < n; i++)
  19.             //{
  20.             //    Console.Write("Element {0} = ", i + 1);
  21.             //    array[i] = Int32.Parse(Console.ReadLine());
  22.             //}
  23.  
  24.             int[] array = { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3 };
  25.             int[] occurrencses = new int[1024];
  26.  
  27.             for (int i = 0; i < array.Length; i++)
  28.             {
  29.                 int n = array[i];
  30.                 occurrencses[n]++;
  31.             }
  32.  
  33.             int mostCommonNumber = 0;
  34.             for (int i = 1; i < occurrencses.Length; i++)
  35.             {
  36.                 if (occurrencses[i] > occurrencses[mostCommonNumber])
  37.                 {
  38.                     mostCommonNumber = i;
  39.                 }
  40.             }
  41.  
  42.             Console.WriteLine("The most common number is {0} and it occurs {1} times",
  43.                 mostCommonNumber,occurrencses[mostCommonNumber]);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment