Advertisement
elena1234

CountSymbols-interview questions

Jan 5th, 2021
186
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 CountSymbols
  6. {
  7.     static class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string text = Console.ReadLine();
  12.             var dictSymbolTimes = new Dictionary<char, int>();
  13.             for (int i = 0; i < text.Length; i++)
  14.             {
  15.                 char symbol = text[i];
  16.                 if (!dictSymbolTimes.ContainsKey(symbol))
  17.                 {
  18.                     dictSymbolTimes.Add(symbol, 1);
  19.                 }
  20.  
  21.                 else
  22.                 {
  23.                     dictSymbolTimes[symbol]++;
  24.                 }
  25.             }
  26.  
  27.             foreach (var (symbol, times) in dictSymbolTimes.OrderBy(symbol => symbol.Key))
  28.             {
  29.                 Console.WriteLine($"{symbol}: {times} time/s");
  30.             }
  31.         }
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement