基本上在面试的时候,会具体到两个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#) 求两个数组的交集的更多相关文章

  1. java用最少循环求两个数组的交集、差集、并集

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List ...

  2. java使用bitmap求两个数组的交集

    一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担.我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数 ...

  3. leetcode-350-Intersection of Two Arrays II(求两个数组的交集)

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  4. 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 ...

  5. 用lua求两个数组的交集、并集和补集。

    -- 克隆 function Clone(object) local lookup_table = { } local function _copy(object) if type(object) ~ ...

  6. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  7. 求两个集合的交集和并集C#

    我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...

  8. VBA/Excel-实例系列-04-求两个数组的交集

    原创: Z Excel高效办公之VBA 2017-03-10 Part 1:逻辑过程 已有两个数组,要求单个数组中信息无重复 以最短的数组作为循环,分别判断该数组中的元素是否在另一个数组中 如果某一元 ...

  9. LeetCode初级算法之数组:350 两个数组的交集 II

    两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...

随机推荐

  1. a标签中的点击事件

    我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这 是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问 ...

  2. spring boot 初试,springboot入门,springboot helloworld例子

    因为项目中使用了spring boot ,之前没接触过,所以写个helloworld玩玩看,当做springboot的一个入门例子.搜索 spring boot.得到官方地址:http://proje ...

  3. 转:Ajax中的get和post两种请求方式的异同

    1. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML ...

  4. Windows下gvim配置

    Windows下gvim配置原作地:http://hi.baidu.com/leemoncc/blog/item/a6be15cf40d7ab31b600c806.html 0.准备软件及插件. (a ...

  5. Java中的异常处理

    描述: 如果Java中的函数有可能抛出异常,则该异常要么被catch住,要么在声明函数时必须声明该函数体会throws exception. 处理的时候的流程是,当发生异常时,首先结束当前函数后续语句 ...

  6. 教你看懂GERBER中的钻孔(.txt)文件

    PCB在制作的时候也会导出相应钻孔的坐标位置,但是发现网上很少有关于这方面的资料,而一些项目中,可能就会用到钻孔的坐标信息,今天就抛下砖. M48 ;Layer_Color=9474304  % 图层 ...

  7. 基于NodeJS的全栈式开发

    前言 为了解决传统Web开发模式带来的各种问题,我们进行了许多尝试,但由于前/后端的物理鸿沟,尝试的方案都大同小异.痛定思痛,今天我们重新思考了“前后端”的定义,引入前端同学都熟悉的 NodeJS,试 ...

  8. Android常见控件— — —EditText

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  9. 简单的线性分类——MATLAB,python3实现

    看李政轩老师讲的Kernel,讲的非常好!前面有几道作业题,用MATLAB简单做了下,不知道对不对,错误之处还请指出. 题目是这样的. 一.MATLAB版本: clear; clc % 生成train ...

  10. 【RobotFramework自动化测试】RFS常用脚本

    读取后台数据文件:Import Variables | ${CURDIR}/\ABC.py 定位页面:Wait Until Keyword Succeeds | 5s | 500ms | select ...