如何解决如何获取两个没有任何对的列表之间的相交值?
我有两个字符串列表,想要比较它们并提取值,以使以下情况成立:
list1 = {0,1,2,3,3}
list2 = {0,3}
预期的O / P:3 。(我需要忽略其他列表中具有对的值,而仅获取剩余的值)。 在大多数情况下,项目顺序都将保持不变,如果我们涵盖了顺序不同的边际情况,那就太好了。
解决方法
尝试以下方法,我想您想要的
List<int> l1 = new List<int>{1,2,3,3};
List<int> l2 = new List<int>{1,4};
List<int> result = new List<int>();
List<int> l1c = new List<int>();
List<int> l2c = new List<int>();
l1c.AddRange(l1);
l2c.AddRange(l2);
foreach(int item in l1)
l2c.Remove(item);
foreach(int item in l2)
l1c.Remove(item);
result.AddRange(l1c);
result.AddRange(l2c);
Console.WriteLine(string.Join(",",result));
// this outputs 3,4
return result;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。