-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKod14_Dictionaries.cs
More file actions
44 lines (38 loc) · 1.4 KB
/
Kod14_Dictionaries.cs
File metadata and controls
44 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Diagnostics;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
/*Задача 14: Dictionary<TKey, TValue> — подсчёт частоты
Задача
Дан массив строк (слова).
Подсчитай, сколько раз каждое слово встречается, и выведи слова и их частоту (в любом порядке).
Пример входа: "cat", "dog", "cat", "bird", "dog", "cat"
Ожидаемый вывод (пример): cat: 3 dog: 2 bird: 1*/
class Program
{
static void Main()
{
Console.WriteLine("Введите животных через пробел: ");
string input = Console.ReadLine();
string[] parts = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, int> AnimalsCount= new Dictionary<string, int>();
foreach (var word in parts)
{
if (AnimalsCount.ContainsKey(word))
{
AnimalsCount[word]++;
}
else
{
AnimalsCount[word] = 1;
}
}
Console.WriteLine("Частота слов:");
foreach (var pair in AnimalsCount)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
}
}