(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/ 给定两个数组,编写一个函数来计算它们的交 ...
随机推荐
- 内嵌页js与ios和安卓的交互
ios: 一个iframe,改变url会发送一个请求,把url设置成就是bridge://xxxxx客户端就可以拦截请求,并在全局变量xxxxx中取出一个字符串.例如{event:'click'},可 ...
- F2工作流引擎模型
工作流引擎(Workflow Engine ) [编辑] 工作流引擎概述 工作流引擎是指workflow(工作流)作为应用系统的一部分,并为之提供对各应用系统有决定作用的根据角色.分工和条件的不同决定 ...
- 超实用的JavaScript技巧及最佳实践
众所周知,JavaScript是一门非常流行的编程语言,开发者用它不仅可以开发出炫丽的Web程序,还可以用它来开发一些移动应用程序(如PhoneGap或Appcelerator),它还有一些服务端实现 ...
- web项目的日志打印位置设置
1, 若在项目中放logback.groovy文件(如: src/test/resource下),则日志会打印到控制台上. logback.groovy 内容如下: // // Built on Fr ...
- terminator终端工具
terminator是个很好的终端程序,在Ubuntu Linux下安装如下: sudo apt-get install terminator 可在同一屏打开多个窗口:
- Java类的成员函数调用顺序
class A { public A() { System.out.println("----------A 构造-------------"); } static void sb ...
- warning: incompatible implicit declaration of built-in function ‘exit’
出现这个错误,一般是程序中某个函数没有include相关的文件. EG. 出现这个错误是因为要使用exit()应该包含stdlib.h文件
- Python的平凡之路(2)
一.标准库(sys & os): Python 的标准库(standard library) 是随着 Python 一起安装在你的电脑中的,是 Python 的一部分 (当然也有特殊情况. ...
- Ubuntu中安装eclipse ,双击eclipse出现invalid configuration location问题
ubuntu invalid configuration location 标签: myeclipse for ubuntu ubuntu myeclipse ubuntu安装myecli ...
- MySQL文件目录格式及存放位置
了解MYSQL的都知道,在MYSQL中建立任何一张数据表,在其数据目录对应的数据库目录下都有对应表的.frm文件,.frm文件是用来保存每个数据表的元数据(meta)信息,包括表结构的定义等,.frm ...