Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string[] inputWords = Console.ReadLine().Split(' ').ToArray();
- int inputWordsCount = inputWords.Length;
- List<KeyValuePair<string, int>> wordsAndCounts =
- new List<KeyValuePair<string, int>>();
- foreach (string word in inputWords)
- {
- if (!wordsAndCounts.Exists(kvp => kvp.Key == word))
- {
- wordsAndCounts.Add(new KeyValuePair<string, int>(word, 1));
- }
- else
- {
- for (int i = 0; i < wordsAndCounts.Count; i++)
- {
- if (wordsAndCounts[i].Key == word)
- {
- int currCount = wordsAndCounts[i].Value;
- wordsAndCounts.RemoveAt(i);
- wordsAndCounts.Insert(i, new KeyValuePair<string, int>
- (word, currCount + 1));
- break;
- }
- }
- }
- }
- var sortedWordsByCounts = wordsAndCounts.OrderByDescending(kvp => kvp.Value);
- foreach (var pair in sortedWordsByCounts)
- {
- double percentage = (double)pair.Value / inputWordsCount * 100;
- Console.WriteLine($"{pair.Key} -> {pair.Value} times ({percentage:F2}%)");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement