Advertisement
AvengersAssemble

Common - Rare

Sep 16th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 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 Practice
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Random rnd = new Random();
  14.             int common = 0, rare = 0, random, smallest = 0, biggest = 0;
  15.             Console.WriteLine("Specify array length:");
  16.             int length = int.Parse(Console.ReadLine());
  17.             int[] arr = new int[length];
  18.             int[] frequency = new int[length];
  19.             for (int i = 0; i < arr.Length; i++)
  20.             {
  21.                 random = rnd.Next(0, 10);
  22.                 arr[i] = random;
  23.             }
  24.             for (int i = 0; i < frequency.Length; i++)
  25.             {
  26.                 frequency[i] = 1;
  27.                 for (int a = 0; a < arr.Length; a++)
  28.                 {
  29.                     if (arr[i] == arr[a])
  30.                         frequency[i]++;
  31.                 }
  32.             }
  33.             for (int i = 0; i < frequency.Length; i++)
  34.             {
  35.                 if (i == 0)
  36.                 {
  37.                     smallest = frequency[i];
  38.                     biggest = frequency[i];
  39.                 }
  40.                 else
  41.                 {
  42.                     if (frequency[i] > biggest)
  43.                         biggest = frequency[i];
  44.                     if (frequency[i] < smallest)
  45.                         smallest = frequency[i];
  46.                 }
  47.             }
  48.             common = Array.IndexOf(frequency, biggest);
  49.             rare = Array.IndexOf(frequency, smallest);
  50.             Console.WriteLine("Common: {0}\nRare: {1}", arr[common], arr[rare]);
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement