Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _04._Array_Histogram
- {
- class ArrayHistogram
- {
- static void Main()
- {
- List<string> words = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .ToList();
- Dictionary<string, int> dict = new Dictionary<string, int>();
- foreach (string word in words)
- {
- if (!dict.ContainsKey(word))
- {
- dict.Add(word, 1);
- }
- else
- {
- dict[word]++;
- }
- }
- Dictionary<string, int> sortedDict = dict
- .OrderByDescending(x => x.Value)
- .ToDictionary(k => k.Key, v => v.Value);
- foreach (var word in sortedDict)
- {
- double percentage = ((double)word.Value / words.Count) * 100.0;
- Console.WriteLine($"{word.Key} -> {word.Value} times ({percentage:f2})");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement