Advertisement
AvengersAssemble

Common - Rare v0.2

Sep 16th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication23
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int size, common=0, rare=0, appearances, appearancesCommon = 1, appearancesRare = 2;
  14.             size = int.Parse(Console.ReadLine());
  15.             int[] arr = new int[size];
  16.             Random rnd = new Random();
  17.             for (int i = 0; i < arr.Length; i++) //sets up array
  18.             {
  19.                 int randomNum = rnd.Next(0, 10);
  20.                 arr[i] = randomNum;
  21.             }
  22.             for (int i = 0; i < arr.Length; i++)
  23.             {
  24.                 appearances = 0;
  25.                 for (int k = 0; k < arr.Length; k++) //counts appearances of each number
  26.                 {
  27.                     if (arr[i] == arr[k])
  28.                     {
  29.                         appearances++;
  30.                     }
  31.                 }
  32.                 if (appearances > appearancesCommon)
  33.                 {
  34.                     appearancesCommon = appearances;
  35.                     common = arr[i];
  36.                 }
  37.                 if (appearances < appearancesRare)
  38.                 {
  39.                     appearancesRare = appearances;
  40.                     rare = arr[i];
  41.                 }
  42.             }
  43.             Console.WriteLine("\nMost common number: {0}. Rarest number: {1}.", common, rare);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement