List<T> 泛型集合应用,C# 找出数组中重复的数字,以及重复数字的次数

 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();

        } 

    }

还有其他的好的方法,请多多指教。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值