svetlozar_kirkov

Most frequently occuring (Exercise)

Oct 1st, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleTesting2
  6. {
  7.     class ConsoleTesting2
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.Write("Enter array length: ");
  12.             int n = int.Parse(Console.ReadLine());
  13.             var ints = new int[n];
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 Console.Write("Index \"{0}\": ", i);
  18.                 ints[i] = int.Parse(Console.ReadLine());
  19.             }
  20.  
  21.             //Array.Sort(ints);
  22.  
  23.             var uniqueNums = ints.Distinct().ToList();
  24.             var counts = new List<int>();
  25.  
  26.  
  27.             for (int i = 0; i < uniqueNums.Count; i++)
  28.             {
  29.                 int count = 0;
  30.  
  31.                 for (int j = 0; j < n; j++)
  32.                 {
  33.                     if (ints[j] == uniqueNums[i])
  34.                     {
  35.                         count++;
  36.                     }
  37.                 }
  38.                 counts.Add(count);
  39.             }
  40.  
  41.             Console.WriteLine("\nUnique nums and their occurences: ");
  42.             for (int i = 0; i < uniqueNums.Count; i++)
  43.             {
  44.                 Console.WriteLine("{0} ({1})", uniqueNums[i], counts[i]);
  45.             }
  46.            
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment