import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set; /**
* 用最少循环求两个数组的交集、差集、并集
*
* @author ZQC
*
*/
public class Test
{ public static void main(String[] args)
{
Integer[] m = { 1, 2, 3, 4, 5 };
Integer[] n = { 3, 4, 6 }; System.out.println("----------并集------------");
Integer[] b = getB(m, n);
for (Integer i : b)
{
System.out.println(i);
} System.out.println("----------交集------------");
Integer[] j = getJ(m, n);
for (Integer i : j)
{
System.out.println(i);
} System.out.println("----------差集------------");
Integer[] c = getC(m, n);
for (Integer i : c)
{
System.out.println(i);
}
} /**
* 求并集
*
* @param m
* @param n
* @return
*/
private static Integer[] getB(Integer[] m, Integer[] n)
{
// 将数组转换为set集合
Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(m));
Set<Integer> set2 = new HashSet<Integer>(Arrays.asList(n)); // 合并两个集合
set1.addAll(set2); Integer[] arr = {};
return set1.toArray(arr);
} /**
* 求交集
*
* @param m
* @param n
* @return
*/
private static Integer[] getJ(Integer[] m, Integer[] n)
{
List<Integer> rs = new ArrayList<Integer>(); // 将较长的数组转换为set
Set<Integer> set = new HashSet<Integer>(Arrays.asList(m.length > n.length ? m : n)); // 遍历较短的数组,实现最少循环
for (Integer i : m.length > n.length ? n : m)
{
if (set.contains(i))
{
rs.add(i);
}
} Integer[] arr = {};
return rs.toArray(arr);
} /**
* 求差集
*
* @param m
* @param n
* @return
*/
private static Integer[] getC(Integer[] m, Integer[] n)
{
// 将较长的数组转换为set
Set<Integer> set = new HashSet<Integer>(Arrays.asList(m.length > n.length ? m : n)); // 遍历较短的数组,实现最少循环
for (Integer i : m.length > n.length ? n : m)
{
// 如果集合里有相同的就删掉,如果没有就将值添加到集合
if (set.contains(i))
{
set.remove(i);
} else
{
set.add(i);
}
} Integer[] arr = {};
return set.toArray(arr);
} }

java用最少循环求两个数组的交集、差集、并集的更多相关文章

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

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

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

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

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

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

  4. java求两个集合的交集和并集,比较器

    求连个集合的交集: import java.util.ArrayList; import java.util.List; public class TestCollection { public st ...

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

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

  6. 求两个list的交集和并集

    两个list的并集,只需去除重复元素即可: 将两个list放入同一个set中即可: 两个list的交集: 1将其中一个list放入set, 2循环另一个list,每次向set塞值, 3判断set的总数 ...

  7. js 获取两个数组的交集,并集,补集,差集

    https://blog.csdn.net/piaojiancong/article/details/98199541 ES5 const arr1 = [1,2,3,4,5], arr2 = [5, ...

  8. (C#) 求两个数组的交集

    基本上在面试的时候,会具体到两个int数组,或string数组.具体也就是讨论算法. 首先需要的是和面试的人确认题目的含义,并非直接答题. 然后,可以提出自己的想法,首先最快的是用linq { Lis ...

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

随机推荐

  1. 爬取虎扑NBA首页主干道推荐贴的一只小爬虫,日常爬不冷笑话解闷

    虎扑是广大jrs的家园,步行街是这个家园里最繁华的地段.据称广大jrs平均学历985,步行街街薪30w起步. 大学时经舍友安利,开始了解虎扑,主要是看看NBA的一些资讯. 偶尔也上上这个破街,看看jr ...

  2. CSS样式规范

    一般团队都有对CSS样式的规范,因为只有写的规范些,维护层本低,易懂.我们开发并不一次性的,往往都是要迭代的,如果这次随便写,下次迭代的时候将付出高昂的代价.而团队的规范一般都大同小异,往往都包含一下 ...

  3. 如何使用VS Code编写Spring Boot (第二弹)

    本篇文章是续<如何使用VS Code编写Spring Boot> 之后,结合自己.net经验捣鼓的小demo,一个简单的CRUD,对于习惯了VS操作模式的.net人员非常方便,强大的智能提 ...

  4. 可用的NTP服务器地址

    国内可用的Internet时间同步服务器地址(NTP时间服务器)好在阿里云提供了7个NTP时间服务器也就是Internet时间同步服务器地址 ntp1.aliyun.comntp2.aliyun.co ...

  5. CentOS7使用firewalld防火墙

    firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status fir ...

  6. kettle —— 从 oracle 到 oracle的坑

    公司有个项目,是使用kettle从oracle上统计,再将结果跟oracle中目标表进行对比更新.接手后,走了一些弯路,中间各种尝试都不尽如人意,也学了kettle的一些组件的用法.正好趁着机会记录 ...

  7. flume1.9 用户指南(中文版)

    概述 Apache Flume是一个分布式,可靠且可用的系统,用于有效地从许多不同的source收集,聚合和移动大量日志数据到集中式数据存储. Apache Flume的使用不仅限于日志数据聚合.由于 ...

  8. JAVA写入大文件DEMO

    /**     * 读取filePath的文件     * @param filePath    文件的路径     * @return     List集合       文件中一行一行的数据     ...

  9. Codeforces 755E:PolandBall and White-Red graph(构造+思维)

    http://codeforces.com/contest/755/problem/E 题意:给出n个点和一个距离d,让你在这个n个点的图里面构造一个子图,使得这个子图的直径和补图的直径的较小值为d, ...

  10. django基础知识之上传图片:

    上传图片 当Django在处理文件上传的时候,文件数据被保存在request.FILES FILES中的每个键为<input type="file" name="& ...