(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/ 给定两个数组,编写一个函数来计算它们的交 ...
随机推荐
- EasyUI-panel 内嵌页面上的js无法被执行
声明: http://www.jeasyuicn.com/post-49.html 本文引用自GodSon的杰作 http://www.jeasyuicn.com/post-49.html,除修正了个 ...
- Microsoft Enterprise Library 5.0 缓存配置
在使用企业库的缓存时遇到一个问题. 创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibr ...
- 为什么高手离不了Linux系统?这就是我的理由
摘要: 通过本文来记录下我在Linux系统的学习经历,聊聊我为什么离不了Linuxx系统,同时也为那些想要尝试Linux而又有所顾忌的用户答疑解惑,下面将为你介绍我所喜欢的Linux系统,这里有一些你 ...
- win7 下设置时间格式为yyyy-MM-dd 格式无效的解决方法
部分win7 64位机器,在时间区域部分设置了时间格式为:yyyy-MM-dd后程序和数据库里面还是原来默认的yyyy/MM/dd格式 打开注册表,搜索 yyyy/MM/dd ,修改为yyyy-MM- ...
- Oracle数据库中scott用户不存在的解决方法
SCOTT用户是我们学习Oracle过程中一个非常重要的实验对象,在我们建立数据库的时候,如果是选择定制模式的话,SCOTT用户是不会默认出现的,不过我们可以通过使用几个简单命令来使这个用户出现.以下 ...
- Centos上的安装openoffice+unoconv+swftools (转)
############################## # swftools的安装 # ############################## 1.安装所需的库和组件 yum ...
- realestate.cei.gov.cn
using AnfleCrawler.Common; using System; using System.Collections.Concurrent; using System.Collectio ...
- Python开发入门与实战6-表单
6. 表单 从简朴的单个搜索框,到常见的Blog评论提交表单,再到复杂的自定义数据输入接口,HTML表单一直是交互性网站的重要交互手段.本章介绍如何用Django如何对用户通过表单提交的数据进行访问. ...
- Unity3d_学习笔记_入门
转自:http://blog.csdn.net/zlfxy/article/details/8722437 本文内容来自“编程教父”的视频课程. 1.Unity3d一个游戏引擎,可以用来开发很多游戏. ...
- exec函数族,守护进程,线程同步和互斥
2015.3.2 进程和程序有三点不同:1,存在位置不同,程序:硬盘,磁盘.进程:内存2. 程序是静态的,进程是动态的 执行./a.out -->bash->bash程序调用fork()- ...