在两个整数列表中查找公共元素 [英] Find common elements in two Integer List

查看:127
本文介绍了在两个整数列表中查找公共元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个给定整数列表:

alist  : TList<Integer>;   //  eg.   1,2,3,4,5,6,7,8,9
blist  : TList<Integer>;   //  e.g   1,2,3,4,5

Resultlist  : TList<Integer>;     

IgnoreList : TList<Integer>;   //  e,g,  1,2,3

找到共同元素的有效方法是什么在两个列表中,不包括忽略列表中的元素。由于我必须在许多项目上运行此过程,因此我需要一种有效且快速的方法来解决此问题。

What is a effective way to find the common elements on both lists, excluding elements from the ignore list. As I have to run this procedure over many items I need a effective and fast way of implementation for this problem.

Resultlist  should be 4,5 


推荐答案

我同意德米特里的意见。无论列表是否已排序,将列表转换为哈希集并查找它们都会很快。

I agree with Dmitry. Converting lists to hash sets and looking up in them would be fast irrespective of whether the lists are sorted.

查看Delphi的TDictionary。 TDictionary交集是寻找共同元素的一种快捷方式。否则,

Have a look at Delphi's TDictionary. TDictionary intersection is one quick way of finding common elements. Otherwise,

1)为列入黑名单的元素创建一个TDictionary。
2)创建一个TDictionary并插入alist中不存在于blacklist-dictionary中的元素。此操作很快,因为TDictionary已针对查找进行了优化。
3)最后,迭代blist的元素,只输出alist-dictionary中的元素。

1) Create a TDictionary for blacklisted elements. 2) Create a TDictionary and insert elements from alist that are not present in blacklist-dictionary. This operation is fast because TDictionary are optimized for lookup. 3) finally, iterate over elements of blist and only output elements preent in alist-dictionary.

这篇关于在两个整数列表中查找公共元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆