class Program
{
static void Main(string[] args)
{
//自定义一个泛型集合
List<int> tlist = new List<int>()
{
4,1,2,10,4,5,6,7,5,4,10,22,33,44,55,4,4,22,30,55,10,10,10,10
};
int b = 1; //记录重复次数
int c = 0; //记录重复数
Hashtable tb = new Hashtable();
for (int i = 0; i < tlist.Count; i++)
{
for (int j = i + 1; j < tlist.Count; j++)
{
if (tlist[i] == tlist[j]) //进行比较
{
c = tlist[i];
b++;
}
}
//如果超过1了表示重复,并且没有被记录过,就打印出来
if (b > 1 && !tb.ContainsKey(c))
{
Console.WriteLine("重复数字为:{0},重复的次数为:{1}", c, b);
tlist.RemoveAll(p => p == c); //从tlist移除重复数,得到不是重复数的集合
tb.Add(c, b);
}
b = 1;
}
string temNum=string.Empty;
//遍历没有重复的数字
for (int i = 0; i < tlist.Count; i++)
{
temNum += tlist[i]+",";
}
Console.WriteLine("没有重复数字的:{0}", temNum.Substring(0,temNum.Length-1));
Console.ReadKey();
}
}
还有其他的好的方法,请多多指教。