基本上在面试的时候,会具体到两个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. CSS3判断手机横屏竖屏

    原理: 当用户旋转屏幕的时候,会进入到你的监听方法中,然后通过window.orientation来获取当前屏幕的状态:0 - 竖屏90 - 逆时针旋转横屏-90 - 顺时针旋转横屏180 - 竖屏, ...

  2. 第三节 构造一个简单的Linux系统MenuOS——20135203齐岳

    第三节 构造一个简单的Linux系统MenuOS By 20135203齐岳 Linux内核源代码 arch/ 支持不同cpu的源代码 Documentations/ 文档存储 init/ 内核启动相 ...

  3. 对比Linux系统和Windows系统哪个更好

    最近半年来,我一直在读一本叫做<Linux就该这么学>的Linux教材,确实让我进步的很快,也慢慢的让Linux系统走入了我的知识认知中,那么学习前我们来对比下Windows和Linux的 ...

  4. Deprecated: Function split() is deprecated in ... 解决办法

    本地测试的程序上传到服务器出现很多错误,Deprecated: Function split() is deprecated  查了原因是因为PHP的版本不同所导致的,本身程序开发的时候用的是PHP5 ...

  5. .Net中的加密与解密

    加密与解密概述 加密与解密属于数据安全的范畴.在消息传输时,通过对消息进行特殊编码(加密),建立一种安全的交流方式,使得只有发送者所期望的接收者能够理解(解密).这里我们定义一个场景:发送方,接收方, ...

  6. Ubuntu下Speedtest的安装

    要安装Speedtest,需要先安装apache,参见<Ubuntu下Apache的安装>一文:*(再安装LAMP server,参见<Ubuntu下快速安装LAMP server& ...

  7. 4、android之actionbar用法

    转: 上:http://blog.csdn.net/yuzhiboyi/article/details/32709833 下:http://blog.csdn.net/yuzhiboyi/articl ...

  8. Jquery EasyUI DataGrid .net实例

    前台界面:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  9. url中的百分号转译

    有一次发现自己输入的url中含有中文的时候,他会转化为%XXXX的格式. 于是想怎么把他给转换回去,于是使用了urllib库 #-*-coding:utf8 -*- import urllib url ...

  10. remove adapter

    Although adapter and other technical sequences can potentially occur in any location within reads, b ...