Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ex1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string text = Console.ReadLine();
- List<string> words = text.Split(new char[] {',', ' ', '.', '!', '?', '-'}).ToList();
- // The laziest solution -> prints how many times every word exists in the given input
- //while (words.Count != 0)
- //{
- // string word = words[0];
- // Console.WriteLine(word + "--> " + words.Count(x => x == word));
- // words.RemoveAll(x => x == word);
- //}
- // This solution prints the most common word in the given input
- int maxCounter = 0;
- string maxWord = string.Empty;
- int currentCounter = 1;
- for (int i = 0; i < words.Count; i++)
- {
- if (HasBeenEarlier(words, i)) // Just optimization
- {
- continue;
- }
- if (maxCounter >= words.Count - i) // Another optimization
- {
- break;
- }
- for (int j = i + 1; j < words.Count; j++)
- {
- if (words[i] == words[j])
- {
- currentCounter += 1;
- }
- }
- if (currentCounter > maxCounter)
- {
- maxCounter = currentCounter;
- maxWord = words[i];
- }
- currentCounter = 1;
- }
- Console.WriteLine("The moast common word is: " + maxWord + " --> " + maxCounter);
- }
- static bool HasBeenEarlier(List<string> words, int border)
- {
- for (int i = 0; i < border; i++)
- {
- if (words[i] == words[border])
- {
- return true;
- }
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement