如何判断两个数组是否共享相同的元素
所以这是我的问题的一种更简单的形式。 假设我有 2 个数组。 A={1,2} 且 B={2,4,6}。 如果 A 和 B 共享一个元素,则从 B 中删除该元素。 我知道您可以循环遍历并将 A 中的每个元素与 B 中的每个元素进行比较,但必须有更好的方法!
So this is a simpler form of my problem.
Lets say I have 2 arrays. A= {1,2} and B={2,4,6}.
If A and B share an element then delete that element from B.
I know you can loop through and compare each element in A to each element in B, but there's got to be a better way!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的数组已排序(或者您可以对数组进行排序),则您可以同时处理两个数组。 其各自数组的末尾之外:
If your arrays are sorted (or you can sort the arrays), you can then work through both arrays simultaneously. Starting at the beginning of both arrays and going until you would advance one of the pointers beyond the end of its respective array:
您必须编写代码,但它不必是强力的双重嵌套循环,并且您不必从数组中删除单个元素。
如果添加对 Microsoft 脚本运行时的引用(从 VBE 中的“工具”菜单),则可以使用“字典”对象来简化此操作。它有“Exists”、“Remove”和“Keys”方法。因此,您可以循环遍历 B 并将元素添加为字典中的键,然后循环遍历 A,检查这些元素是否存在,如果存在,则删除它们。
作为伪代码:
上述方法还会从 B 中删除重复项(如果有)。
如果您知道数组没有错误值作为元素,则还可以使用 MATCH(以及在 VBA 'Application.Match' 或 'Application.WorksheetFunction.Match' 中)。执行类似操作
将返回 {2,#N/A,#N/A}。任何带有 #N/A 的位置都是 B 中不在 A 中的元素的位置。如果您在工作表中进行匹配,则可以拖动类似的公式
,然后从中过滤掉 #N/As。在VBA中,你可以这样做(更多伪代码):
当然,然后你必须担心数组起始边界等。
You have to write code, but it doesn't have to be a brute-force doubly nested loop and you don't have to do any messy removal of individual elements from arrays.
If you add a reference to the Microsoft Scripting Runtime (from the Tools menu in the VBE), you can use a Dictionary object to make this easier. It has methods for 'Exists', 'Remove', and 'Keys'. So you could loop through B and add the elements as keys in the Dictionary, and then loop through A, checking to see if those elements exist, and if so, removing them.
As pseudocode:
The above approach also removes duplicates from B if there are any.
If you know your arrays don't have error values as elements, you can also use MATCH (and in VBA 'Application.Match' or 'Application.WorksheetFunction.Match'). Doing something like
will return {2,#N/A,#N/A}. Any position with #N/A is the position of an element of B that wasn't in A. If you do the match in the worksheet, you can then drag a formula like
and then filter out the #N/As from that. In VBA, you could do (more pseudocode):
Of course, then you have to worry about array starting bounds, etc.
基于 jtolle 伪代码的完整且解释的 VBA 解决方案
通过 1 行
Application.Match()
帮助函数getUniques()
< /strong>//编辑 2020-07-24 根据 @jtolle 的评论更正了数组边界:
A completed and explained VBA solution based on jtolle's pseudo code
Help function
getUniques()
via 1-lineApplication.Match()
//Edit 2020-07-24 corrected array boundaries due to @jtolle 's comment:
不,我认为没有。你必须循环遍历。
No there is not I think. You have to loop through.
如果数组内的值对于其数组来说是唯一的,则可以使数组索引实际值,从而允许您直接查找其索引,而不是扫描整个数组。
从元素较少的数组开始,我假设您想要执行 excel/VB?我制作了一张图片来说明这个想法。
http://img694.imageshack.us/img694/1503/hackmap.jpg
您没有两个嵌套循环,而是一个循环,并且它仅迭代与最小数组一样多的次数。
If values inside a array are unique for its array, you could make the array indexes the actual values, allowing you to seek directly to its index, instead of scanning the entire array.
Start with the array that has fewer elements, and i assume you are wanting to do excel/VB? I made a picture to illustrate the idea.
http://img694.imageshack.us/img694/1503/hackmap.jpg
Instead of having two nested loops, you have one loop, and it only iterates as many times as the smallest array.