(C#) 求两个数组的交集
基本上在面试的时候,会具体到两个int数组,或string数组。具体也就是讨论算法。
首先需要的是和面试的人确认题目的含义,并非直接答题。
然后,可以提出自己的想法,首先最快的是用linq
- {
- List<int> array0 = new List<int>() { , , , , , };
- List<int> array1 = new List<int>() { , , };
- List<int> arrayInterSect = array0.Intersect(array1).ToList(); // 交集
最好写个函数:
- public List<int> GetInterSect(List<int> array0, List<int> array1)
- {
- return array0.Intersect(array1).ToList();
- }
如果是差集合并集的话,可以用如下方法解决:
- List<int> arrayExcept = array0.Except(array1).ToList(); // 差集
- List<int> arrayUnion = array0.Union(array1).ToList(); // 并集
当然,考算法的话,还需要进一步进行。
基本思路可以口头说明是用两个for 循环,逐个匹配。 T(n) = O(n^2); 不要实现,因为O(n^2)的算法不好。
其次稍好的方法可以先快排数组,然后两边同时开始遍历数组。时间复杂度是 O(nlogn).
O(n)的算法才是期望的答案。可以采用Hashtable, 或者Dictionary.
- // The values in array0/array1 must be unique.
- public static List<int> GetIntersect(List<int> array0, List<int> array1)
- {
- Dictionary<int, int> dicIntersect = new Dictionary<int, int>();
- List<int> intersectData = new List<int>();
- // Traverse the first array.
- foreach (var data in array0)
- {
- if (!dicIntersect.Keys.Contains(data))
- {
- dicIntersect.Add(data, );
- }
- dicIntersect[data]++;
- }
- // Traverse the second array.
- foreach (var data in array1)
- {
- if (!dicIntersect.Keys.Contains(data))
- {
- dicIntersect.Add(data, );
- }
- dicIntersect[data]++;
- }
- // Traverse the dictionary to find the duplicated values.
- foreach (var intData in dicIntersect)
- {
- if (intData.Value > )
- {
- intersectData.Add(intData.Key);
- }
- }
- return intersectData;
- }
- }
(C#) 求两个数组的交集的更多相关文章
- java用最少循环求两个数组的交集、差集、并集
import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List ...
- java使用bitmap求两个数组的交集
一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担.我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数 ...
- leetcode-350-Intersection of Two Arrays II(求两个数组的交集)
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
- js求两个数组的交集|并集|差集|去重
let a = [1,2,3], b= [2, 4, 5]; 1.差集 (a-b 差集:属于a但不属于b的集合) a-b = [1,3] (b-a 差集:属于b但不属于a的集合) b-a = [4 ...
- 用lua求两个数组的交集、并集和补集。
-- 克隆 function Clone(object) local lookup_table = { } local function _copy(object) if type(object) ~ ...
- js取两个数组的交集|差集|并集|补集|去重示例代码
http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...
- 求两个集合的交集和并集C#
我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...
- VBA/Excel-实例系列-04-求两个数组的交集
原创: Z Excel高效办公之VBA 2017-03-10 Part 1:逻辑过程 已有两个数组,要求单个数组中信息无重复 以最短的数组作为循环,分别判断该数组中的元素是否在另一个数组中 如果某一元 ...
- LeetCode初级算法之数组:350 两个数组的交集 II
两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...
随机推荐
- JavaScipt 源码解析 数据缓存
常见的内存泄露的几种情况: 循环引用 JavaScript闭包 DOM插入 一个DOM对象被一个JavaScript对象引用,同时又引用同一个或其他的JavaScript对象,这个DOM对象可能回引发 ...
- systemctl命令
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...
- 基于TF-IDF值的汉语语义消歧算法
RT,学校课题需要233,没了 话说,窝直接做个链接的集合好了,方便以后查找 特征值提取之 -- TF-IDF值的简单介绍 汉语语义消歧之 -- 句子相似度 汉语语义消歧之 -- 词义消歧简介 c++ ...
- SEO命令之”site“运用详解
一.“site”基本介绍: 都知道要想查询一个特定网站的收录状况一般会分为两种情况:一.结果中有返回数据,则表明该网站已被收录:二.如果返回数据为空,则该网站未被收录.如果是以前已被收录的,现在来查没 ...
- iOS开发拓展篇—UIDynamic(捕捉行为)
iOS开发拓展篇—UIDynamic(捕捉行为) 一.简介 可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动 UISnapBehavior的初始化 - (instancetype ...
- JSP常见指令
JSP常见指令 标签: jspincludeservletjavaappletarchive 2011-11-07 20:07 13193人阅读 评论(3) 收藏 举报 版权声明:本文为博主原创文章, ...
- 拓扑编号 vijos1790
题意就是拓扑排序,要求1的序号尽可能小,然后2的序号尽可能小,3,4... 一开始很容易想到直接贪心,每次选一个入度为0的点,如果有多个,就选编号最小的那个,但是很容易找到反例. 看了下题解,应该是反 ...
- 读取配置文件 PropertyPlaceholderConfigurer 的配置与使用
public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map< ...
- 2016 - 1 - 24 CSS初步
1.The difference between CSS and HTML HTML document is that it specities the content of the page. An ...
- RBL开发笔记三
2014-08-26 20:06:24 今天就是在开发这个EPOLL来处理网络事件 封装较为健壮的EPOLL模型来处理基本的网络IO 1) 超时这个主题先没有弄 在开发EPOLL包括select/po ...