Advertisement
Aborigenius

ArrayHistogram

Jul 2nd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 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 ArrayHistogram
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] array = Console.ReadLine().Split(' ').ToArray();
  14.  
  15.             List<string> words = new List<string>();
  16.             List<int> counts = new List<int>();
  17.  
  18.             for (int i = 0; i < array.Length; i++)
  19.             {
  20.                 if (!words.Contains(array[i]))
  21.                 {
  22.                     words.Add(array[i]);
  23.                     counts.Add(1);
  24.                 }
  25.                 else
  26.                 {
  27.                     int index = words.IndexOf(array[i]);
  28.                     counts[index]++;
  29.                 }
  30.             }
  31.             for (int firstUnsorted = 0; firstUnsorted < counts.Count - 1; firstUnsorted++)
  32.             {
  33.                 var i = firstUnsorted + 1;
  34.                 while (i > 0)
  35.                 {
  36.                     if (counts[i - 1] <counts[i])
  37.                     {
  38.                         int temp = counts[i];
  39.                         counts[i] = counts[i -1];
  40.                         counts[i -1] = temp;
  41.  
  42.                         string tempWord = words[i];
  43.                         words[i] = words[i - 1];
  44.                         words[i - 1] = tempWord;
  45.                     }
  46.                     i--;
  47.                
  48.                 }
  49.             }
  50.  
  51.             for (int i = 0; i < words.Count; i++)
  52.             {
  53.                 Console.WriteLine("{0} -> {1} times ({2:f2}%)", words[i], counts[i],
  54.                     ((double)counts[i] / (double) array.Length) *100);
  55.             }
  56.  
  57.         }
  58.     }
  59. }
  60.  
  61. /*
  62.             Dictionary<string, int> blah = new Dictionary<string, int>();
  63.  
  64.  
  65.             foreach (string item in array)
  66.             {
  67.                 if (item != "")
  68.                 {
  69.                     if (blah.ContainsKey(item) == false)
  70.                     {
  71.                         blah.Add(item, 1);
  72.                     }
  73.                     else
  74.                     {
  75.                         blah[item]++;
  76.                     }
  77.                 }
  78.             }
  79.  
  80.             foreach (var item in blah)
  81.             {
  82.                 Console.WriteLine(item);
  83.             }
  84. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement