Advertisement
Guest User

CountOfNames

a guest
Apr 9th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _12_CountOfNames
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] names = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  12.             Dictionary<string, int> dictionary = new Dictionary<string, int>();
  13.  
  14.             foreach (var name in names)
  15.             {
  16.                 int count = 0;
  17.                 if (dictionary.ContainsKey(name))
  18.                 {
  19.                     count = dictionary[name];
  20.                 }
  21.                 dictionary[name] = count + 1;
  22.             }
  23.  
  24.             foreach (KeyValuePair<string, int> item in dictionary.OrderBy(key => key.ToString()))
  25.             {
  26.                 Console.WriteLine("{0} -> {1} times", item.Key, item.Value);
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement