(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/ 给定两个数组,编写一个函数来计算它们的交 ...
随机推荐
- OLE填充EXCEL 多SHEET
"1 设置行高 "参数说明:行/列号.行高/列宽.R-行 C-列 FORM row_column USING p_r p_width p_type. CASE p_type. WH ...
- redis教程
windows下安装redis: http://jingyan.baidu.com/article/49ad8bce40174f5834d8fa24.html redis教程: http://www. ...
- pwnable.kr-collision
题目: 链接后登陆 ssh col@pwnable.kr -p2222 查看文件以及权限 Ls –al 查看代码 cat col.c 根据 if(strlen(argv[1]) != 20){ pri ...
- java面试准备之基础排序——冒泡与选择排序
选择排序: [java] public void select(int[] arr){ for(int i=0;i<arr.length;i++){ ...
- window2008 r2 负载均衡
两台服务器win2008 r2 ,iis7.5 ip地址192.168.5.16, 192.168.5.18 虚拟ip 192.168.5.30 设置过程: 1.在两台服务器上安装负载均衡模块 ...
- C/C++语言 预处理小结
预处理功能主要包括宏定义,文件包含,条件编译三部分.分别对应宏定义命令,文件包含命令,条件编译命令三部分实现. 预处理过程读入源代码,检查包含预处理指令的语句和宏定义,并对源代码进行响应的转换.预处理 ...
- UE4 - C++ 射线捕捉
#include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h" //省略大部分代码 void AMyFPS_Charact ...
- linux命令:locate
1.命令介绍: locate用来查找文件,它是在系统的数据库中查找,所以速度非常快. 2.命令格式: locate [选项] 模式 ---这里的模式是指正则表达式 3.命令参数: -e ...
- HDU5000 (DP + 规律)
题意:举例子好说点,告诉你4个数字,8,6,4,2四个数字,组成一个四位数,如果两个数字分别是1111,2222,则2222会吧1111杀掉,就是组成的四位数不能每一位都小于或等于一个数,然后让你求出 ...
- Windows下利用py2exe生成静默运行的命令行程序
py2exe是python的第三方库,可以利用它将你的python脚本编译成可执行文件(exe),而在实际的开发过程中生成的dos窗口很影响用户体验,建议按以下方式让exe静默运行. 首先将你的pyt ...