C#对多个集合和数组的操作(合并,去重,判断)

在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数.

首先举例2个集合A,B.

 List<int> listA = new List<int> {1,2,3,5,7,9};
 List<int> listB = new List<int> {13,4,17,29,2};

 
 listA.AddRange(listB );把集合A.B合并
 List<int> Result = listA.Union(listB).ToList<int>();          //剔除重复项 
 List<int> Result = listA.Concat(listB).ToList<int>();        //保留重复项

 listA.BinarySearch("1");//判断集合中是否包含某个值.如果包含则返回0

在举例两个数组

  int[] i=new int[]{1,2};
  int[] j=new int[]{2,3};
  List<int> r = new List<int>();  

  r.AddRange(i);
  r.AddRange(j);

  int[] c = r.ToArray(); 合并数组

  int[] x=i.Union(j).ToArray<int>(); //剔除重复项 

  int[] x=i.Concat(j).ToArray<int>(); //保留重复项

  int n = Array.BinarySearch(i,3);//判断数组中是否包含某个值.如果包含则返回0

C#高效率对List去重

核心想法是利用hashtable不可以存在相同键的特性,将list里的每一条数据分循环入hashtable里的key,value,遇到相同的数据即可跳过,循环完毕后去重的数据全部存放在hashtable内,如果需要将去重的数据放入list,可以new一个新的list,将去重的数据放入新的list

        Stopwatch watch = null;
        protected void Page_Load(object sender, EventArgs e)
        {
        
            List<string> yuanList = new List<string>() { "World", "Hello", "Help", "shanghai", "World", "Help", "World", "Help", "World", "WWW", "NET"};

            List<string> copyList = new List<string>();
            
            //hashtable的键必须是唯一的,没有有效的排序,它进行的是内在的排序
            Hashtable hashtable = new Hashtable();

            //键必须是唯一的,而值不需要唯一的 
            Dictionary<string, string> dic = new Dictionary<string, string>();

            //元素是唯一的
            HashSet<string> hset = new HashSet<string>() {  };


            watch = Stopwatch.StartNew();
            foreach (string str in yuanList)//源LIST去重
            {
                //方式一
                if (!hashtable.ContainsKey(str))
                {
                    hashtable.Add(str, str);
                    copyList.Add(str);//把不重复的列加入
                }
            }
            Debug.Write("Hashtable花时间:".PadLeft(15) + watch.Elapsed);


            watch = Stopwatch.StartNew();
            foreach (string str in yuanList)//源LIST去重
            {
                //方式二
                if (!dic.ContainsKey(str))
                {
                    dic.Add(str, str);
                    copyList.Add(str);//把不重复的列加入
                }

            }
            Debug.Write("Dictionary花时间:".PadLeft(15) + watch.Elapsed);

            watch = Stopwatch.StartNew();
            foreach (string str in yuanList)//源LIST去重
            {

                //方式三
                if (!hset.Contains(str))
                {
                    hset.Add(str);
                    copyList.Add(str);//把不重复的列加入
                }
            }
            Debug.Write("HashSet花时间:".PadLeft(15) + watch.Elapsed);

            //copyList.ForEach(s=>Response.Write(s+"|"));
        }

C#一维string数组去掉去重算法

   public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            list.Add("11");
            list.Add("11");
            // 全部是11,进行调试可以看出j--效果
            //list.Add("11");
            //list.Add("11");
            //list.Add("11");
            //list.Add("11");
            //list.Add("11");
            //list.Add("11");
            //list.Add("11");
            
            list.Add("21");
            list.Add("11");
            list.Add("31");
            list.Add("31"); 
            Purge(ref list);
            list.ForEach(i => Response.Write(i+"|"));
            
        }

        public static void Purge(ref List<string> needToPurge)
        {

            for (int i = 0; i < needToPurge.Count - 1; i++)
            {
                string deststring = needToPurge[i];
                for (int j = i + 1; j < needToPurge.Count; j++)
                {
                    if (deststring.CompareTo(needToPurge[j]) == 0)
                    {
                        needToPurge.RemoveAt(j);
                        j--; //删除一个元素后,不需要索引后移,继续比较当前索引的新元素
                        continue;
                    }
                }
            }
        }
    }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值