让他每次都遍历最短的数组速度就提升起来了
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
String sentence = "May the world be more gentle and may you be less sad";
String[] array= sentence.ToLower().Split(" ");
List<String> list = new List<String>();
find(array, list);
Console.WriteLine("句子:"+sentence);
Console.ReadKey();
}
public static void find(String[] vs, List<String> list)
{
string sc = vs[0];
int o = 0;
foreach(String c in vs)
{
if(c.Equals(sc))
{
o++;
}
else
{
list.Add(c);
}
}
Console.WriteLine("句子单词:" + sc+"出现了"+o+"次");
listc(list);
}
public static void listc(List<String> list)
{
List<String> lists = new List<String>();
string tt = null;
try
{
tt = list[0];
}
catch
{
return;
}
int op = 0;
foreach (String jk in list)
{
if (jk.Equals(tt))
{
op++;
}
else
{
lists.Add(jk);
}
}
Console.WriteLine("句子单词:" + tt + "出现了" + op + "次");
if (!tt.Equals(""))
{
listc(lists);
}
}
}
}