帮我用C#语言写一段不同字符串中出现的高频词的程序。
时间: 2024-03-12 12:44:22 浏览: 58
好的,您可以参考以下代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
namespace HighFrequencyWords
{
class Program
{
static void Main(string[] args)
{
string[] strings = { "hello world", "world is beautiful", "hello beautiful world" };
Dictionary<string, int> wordCount = new Dictionary<string, int>();
// 统计每个单词的出现次数
foreach (string str in strings)
{
string[] words = str.Split(' ');
foreach (string word in words)
{
if (wordCount.ContainsKey(word))
{
wordCount[word]++;
}
else
{
wordCount[word] = 1;
}
}
}
// 找出出现次数最多的单词
int maxCount = wordCount.Values.Max();
var mostFrequentWords = wordCount.Where(x => x.Value == maxCount).Select(x => x.Key);
// 输出结果
Console.WriteLine("出现次数最多的单词为:");
foreach (string word in mostFrequentWords)
{
Console.WriteLine(word);
}
}
}
}
```
这段代码会在输入的字符串中找出出现次数最多的单词并输出。您可以根据需要对代码进行修改和优化。
阅读全文