Advertisement
Guest User

04. Array Histogram

a guest
Feb 12th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04._Array_Histogram
  6. {
  7.     class ArrayHistogram
  8.     {
  9.         static void Main()
  10.         {
  11.             List<string> words = Console.ReadLine()
  12.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.             Dictionary<string, int> dict = new Dictionary<string, int>();
  15.  
  16.             foreach (string word in words)
  17.             {
  18.                 if (!dict.ContainsKey(word))
  19.                 {
  20.                     dict.Add(word, 1);
  21.                 }
  22.                 else
  23.                 {
  24.                     dict[word]++;
  25.                 }
  26.             }
  27.  
  28.             Dictionary<string, int> sortedDict = dict
  29.                 .OrderByDescending(x => x.Value)
  30.                 .ToDictionary(k => k.Key, v => v.Value);
  31.  
  32.             foreach (var word in sortedDict)
  33.             {
  34.                 double percentage = ((double)word.Value / words.Count) * 100.0;
  35.                 Console.WriteLine($"{word.Key} -> {word.Value} times ({percentage:f2})");
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement