比较两个数组或数组列表,找到相似和不同的值
我有两个字符串数组(或者数组列表,如果更容易的话)。我需要比较它们,找到哪些只存在于第一个数组中,哪些同时存在于两个数组中,哪些只存在于第二个数组中。这些数组的长度不同,并且顺序可能不同。如果有必要,我想我可以对它们进行排序......
我知道我可以将它们组合在一起,但我认为这可能有一个相当标准和有效/“最佳”的解决方案,而且我比任何事情都更好奇。
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果数组很大,那么您将需要使用对这些操作有效的数据结构;数组则不然。
如果数组的大小为 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.