比较两个数组或数组列表,找到相似和不同的值

发布于 2024-09-10 05:13:25 字数 255 浏览 3 评论 0原文

我有两个字符串数组(或者数组列表,如果更容易的话)。我需要比较它们,找到哪些只存在于第一个数组中,哪些同时存在于两个数组中,哪些只存在于第二个数组中。这些数组的长度不同,并且顺序可能不同。如果有必要,我想我可以对它们进行排序......

我知道我可以将它们组合在一起,但我认为这可能有一个相当标准和有效/“最佳”的解决方案,而且我比任何事情都更好奇。

我使用 C# 来实现此目的,但如果您想用其他语言编写解决方案,欢迎提供任何帮助。

感谢您的帮助!

I have two arrays (or arraylists if it is easier) of strings. I need to compare these, find which only exist in the first array, which exist in both, and which only exist in the second array. These arrays are different lengths, and may be in different orders. If necessary, I suppose I could sort them...

I know I could hack this together, but I think this might have a fairly standard and efficient / "best" solution, and I am curious more than anything.

I am using c# for this, but if you want to write your solution in another language, any help is welcome.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与清晨阳光 2024-09-17 05:13:25

如果数组很大,那么您将需要使用对这些操作有效的数据结构;数组则不然。

如果数组的大小为 n,则简单的解决方案时间复杂度为 O(n^2)。

如果您对数组进行排序,那么您可以对它们进行二分搜索以查找项目;排序可能是 O(n lg n),并且每次搜索花费 lg n 的成本搜索 n 次,时间也将是 O(n lg n)。

如果您首先将每个数组转换为 HashSet,那么您可以在 O(n) 时间和 O(n) 额外空间内完成此操作。

If the arrays are large then you'll want to use a data structure that is efficient for these operations; arrays are not.

The naive solution is O(n^2) in time if the arrays are of size n.

If you sort the arrays in place then you can binary search them for the items; sorting will likely be O(n lg n) and searching n times at a cost of lg n per search will also be O(n lg n) in time.

If you turn each array into a HashSet<T> first then you can do it in O(n) time and O(n) extra space.

情深缘浅 2024-09-17 05:13:25
var onlyinfirst = from s in list1 where !list2.Contains(s) select s;
var onlyinsecond = from s in list2 where !list1.Contains(s) select s;
var onboth = from s in list1 where list2.Contains(s) select s;
var onlyinfirst = from s in list1 where !list2.Contains(s) select s;
var onlyinsecond = from s in list2 where !list1.Contains(s) select s;
var onboth = from s in list1 where list2.Contains(s) select s;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文