Liststring[] 如何去重
List<string[]> 如何去重,代码如下:
static void Main(string[] args){List<string[]> list = new List<string[]>();list.Add(new string[] { "1", "2", "3" });list.Add(new string[] { "1" });list.Add(new string[] { "1", "2", "3" });list.Add(new string[] { "1" });list.Add(new string[] { "1" });List<string> strList = new List<string>();foreach (var item in list){string s = string.Join(",", item);strList.Add(s);}//要删除的元素的下标集合List<int> removeIndexList = new List<int>();if (list.Count >= 2) //确保下标i不越界 {string currentStr = string.Empty;for (int i = 0; i < strList.Count; i++){currentStr = strList[i];for (int j = i + 1; j < strList.Count; j++){if (currentStr == strList[j]){//添加到要删除的索引集合removeIndexList中 removeIndexList.Add(j);}}}removeIndexList = removeIndexList.Distinct().ToList();////去除重复的索引//添加到要删除的对象集合List<string[]> removeList = new List<string[]>();foreach (var index in removeIndexList){removeList.Add(list[index]);}//遍历要删除对象的集合,删除原集合中的对象foreach (var item in removeList){list.Remove(item);}foreach (var item in list){string s = string.Join(",", item);Console.WriteLine(s);}Console.ReadKey();}}
View Code
运行截图如下:
那么问题又来了,挖掘机技术……呸! 如果是List<List<string[]>>的集合又该如何去重呢?
原理是一样的把List<string[]>变成字符串,装到List<string>中,根据List<sting>重复的元素的下标索引,删除原集合中重复的元素,
代码如下:
static void Main(string[] args){List<string[]> list = new List<string[]>();list.Add(new string[]{"1","2","3"});list.Add(new string[] { "1","2" ,"3"});list.Add(new string[] { "1" });list.Add(new string[] { "1" });list.Add(new string[] { "1" });List<string[]> list2 = new List<string[]>();list2.Add(new string[] { "1", "2", "3", "4", "5" });list2.Add(new string[] { "1", "2", "3" });list2.Add(new string[] { "1" });list2.Add(new string[] { "1" });list2.Add(new string[] { "1" });List<string[]> list3 = new List<string[]>();list3.Add(new string[] { "1", "2", "3" });list3.Add(new string[] { "1", "2", "3" });list3.Add(new string[] { "1" });list3.Add(new string[] { "1" });list3.Add(new string[] { "1" });List<string[]> list4= new List<string[]>();list4.Add(new string[] { "1", "2", "3", "4", "5" });list4.Add(new string[] { "1", "2", "3" });list4.Add(new string[] { "1" });list4.Add(new string[] { "1" });list4.Add(new string[] { "1" });List<List<string[]>> superList = new List<List<string[]>>();//集合list和集合list3是相同,list2和list4相同,并且list4添加了2次 superList.Add(list);superList.Add(list2);superList.Add(list3);superList.Add(list4);superList.Add(list4);List<string> strList = new List<string>();foreach (var d in superList){StringBuilder sb = new StringBuilder();foreach (var dd in d){string s = string.Join(",", dd);sb.Append(s);}string str = sb.ToString();strList.Add(str); //把superList中每个子元素拼接成一条字符串放到strList中 }//要删除的元素的下标集合List<int> removeIndexList = new List<int>();if (strList.Count >= 2) //有2个以上的元素才有可能出现重复 {string currentStr = string.Empty;for (int i =0; i < strList.Count; i++){currentStr = strList[i];for (int j =i+1; j < strList.Count; j++){if (currentStr == strList[j]){//添加到要删除的索引集合removeIndexList中 removeIndexList.Add(j);}}}}removeIndexList = removeIndexList.Distinct().ToList();//去除重复的索引//要删除的对象集合List<List<string[]>> superRemoveList = new List<List<string[]>>();foreach (var index in removeIndexList){superRemoveList.Add(superList[index]);}foreach (var item in superRemoveList){superList.Remove(item);}Console.WriteLine(superList.Count());Console.ReadKey();}
View Code
运行截图如下:
转载于:https://www.cnblogs.com/527289276qq/p/4590395.html
Liststring[] 如何去重相关推荐
- python中排序英文单词怎么写_Python实现对文件进行单词划分并去重排序操作示例...
本文实例讲述了Python实现对文件进行单词划分并去重排序操作.,具体如下: 文件名:test1.txt 文件内容: But soft what light through yonder window ...
- 对列表去重并保持原来的顺序
首先,定义一个列表,即原列表: list1 = [0, 3, 2, 3, 1, 0, 9, 8, 9, 7]: 然后,使用set()对原列表去重list2 = list(set(list1)),得到[ ...
- Array 数组去重 总结10方法(7)
1,常规双循环去重(缺点:循环次数较多) Array.prototype.unique1 = function(){if(this === null){throw new TypeError('&qu ...
- mysql 去重con_python 爬虫 实现增量去重和定时爬取实例
前言: 在爬虫过程中,我们可能需要重复的爬取同一个网站,为了避免重复的数据存入我们的数据库中 通过实现增量去重 去解决这一问题 本文还针对了那些需要实时更新的网站 增加了一个定时爬取的功能: 本文作者 ...
- java字符串去重复_Java 8新特性:字符串去重
本文首发与InfoQ. 8月19日,Oracle发布了JDK 8u20,JDK 8u20包含很多新特性,比如Java编译器更新.支持在运行时通过API来修改MinHeapFreeRatio和MaxHe ...
- stream流map 多个字段_stream流根据对象指定字段去重
先封装一个去重的方法 import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.fun ...
- 字符串去重、统计不同字符种类数问题
1.编写一个函数,计算字符串中含有的不同字符的个数.字符在ACSII码范围内(0~127),不在范围内的不作统计. #include <iostream> #include <str ...
- Java测试List<Object>根据其某个属性去重俩种方法效率
需要用到根据gpsTime double类型对List进行去重: 尝试了俩种办法,就像知道耗时与性能: 1. 法一: // 根据gpsTime去重 imagePostList = imagePostL ...
- javascript ES6有趣的Set,数组去重、并集、交集、差集
Set 对象存储的值总是唯一的 Set 对象方法 方法 描述 add 添加某个值,返回Set对象本身. clear 删除所有的键/值对,没有返回值. delete 删除某个键,返回true.如果删除失 ...
最新文章
- SpringBoot第十五篇:Springboot整合RabbitMQ
- android binder
- android 之UI 高级控件Adapter(适配器详解)
- 强强联手 SAP Ariba与苏宁易购共建中国企业智慧采购
- C语言——输出9*9口诀
- 助力高校数字化建设,QQ小程序开发大赛正式启动
- oracle pfile 注释,Oracle pfile/spfile参数文件详解
- MEF: MSDN 杂志上的文章(6) 一个部件可以有多个导出 !!!
- python皮卡丘编程代码_Python高级编程-(Part 6 部署代码)
- 20200714每日一句
- 报表工具(报表设计器)使用的开发历程
- 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java小型超市货物仓储管理系统w8wry
- 彩色图片如何转为单色位图bmp :用window画板
- 使用开源的协同办公OA项目,实现规范高效的公文管理
- Mybatis中mapper.xml中like模糊查询字符串,日期写法
- Flex框架(The Flex Framework)
- 基层管理者项目管理二三事
- TIPTOP 4GL——自定义按钮前加图标
- 基于SqlServer的DML(数据查询)实验,掌握select查询语句的使用、掌握有无条件查询、结果排序与分组、掌握视图用法
- 金山滨海地图导览(上海南至金山卫铁路22号线)