兩個陣列的並集、交集、差集
const arr1 = [1,2,3];
const arr2 = [3,4,5];
// 並集
const union = Array.from(new Set([...arr1,...arr2])); // union = [1,2,3,4,5]
// 交集
const intersection = Array.from(new Set([...arr1].filter(x => new Set(arr2).has(x)))); // intersection = [3]
// 差集
const difference1 = Array.from(new Set([...arr1].filter(x => !new Set(arr2).has(x)))); // difference1
const difference2= Array.from(new Set([...arr2].filter(x => !new Set(arr1).has(x)))); // difference2 = [4,5]
相關推薦
php獲取兩個陣列相同的元素(交集)以及比較兩個陣列中不同的元素(差集)
(一)php獲取兩個陣列相同元素 array array_intersect(array $array1, array $array2, [, array $...]) array array_int
求兩個有序連結串列的交集和差集
typedef struct ListNode { DataType data; ListNode* next; }ListNode; //交集(注意:list1和list2如果各有2個4,交集也會出現兩次) ListNode* Intersection(ListN
求兩個Linux文字檔案的交集、差集、並集
一、交集 sort a.txt b.txt | uniq -d 二、並集 sort a.txt b.txt | uniq 三、差集 a.txt-b.txt: sort a.txt b.txt b.txt | uniq -u b.txt - a.txt: s
php中陣列的並集、交集和差集函式
計算陣列的並集 array_merge ( array array1[,arrayarray1[,array… ] ) 將一個或多個數組的單元合併起來,一個數組中的值附加在前一個數組的後面。返回作為結果的陣列。 計算陣列的交集 array_intersect ( a
利用sort和uniq求兩個文件的並集,交集和差集
sort uniq 利用sort和uniq求兩個文件的並集,交集和差集 並集:cat file1.txt file2.txt | sort | uniq > file.txt 交集:cat file1.txt file2.txt | sort | uniq -d >file.txt
PHP陣列的幾個操作,求並集,交集,差集,陣列與字串的相互轉換及陣列去重
在php中如果我想要對兩個陣列進行如並集、交集和差集操作,我們可直接使用php自帶的函式來操作如array_merge(),array_intersect(),array_diff(). 計算陣列的合併 array_merge與“+”的區別 array_merge() 函式
shell中使用sort和uniq實現兩檔案的並集,交集和差集
網上轉的,不錯,比使用awk容易點 給定兩個檔案 a.txt 和 b.txt ,每行是一個記錄(假設沒有重複),要求輸出兩集合的交集、並集、差集,輸出的結果只包括唯一項。交集定義為同時出現在兩個檔案中的記錄項,並集定義為出現在任何一個檔案中的記錄項,差集(A-B)定義為出現在A中而且不出現在B中的記
JS陣列求並集,交集和差集
es7: filter結合includes // 並集 let union = a.concat(b.filter(v => !a.includes(v))) // [1,2,3,4,5] // 交集 let intersectio
合併兩個陣列並去重(ES5和ES6兩種方式實現)
ES6實現方式 let arr1 = [1, 1, 2, 3, 6, 9, 5, 5, 4] let arr2 = [1, 2, 5, 4, 9, 7, 7, 8, 8] f
合併兩個陣列並去重
public static void main(String[] a){ String[] first = {"1","4","5","9"}; String[] second = {"6","4","5","7"}; //合併兩個陣列 String[] temp = (String[])
JS實現並集,交集和差集
交集 clas 差集 log set nio bsp csdn 實現 var set1 = new Set([1,2,3]);var set2 = new Set([2,3,4]); 並集let union = new Set([...set1, ...set2]); 交集
【Mysql】求兩個表(查詢結果)的差集
求兩個查詢結果的差集,Mysql直接提供了關鍵字not in,但是not in的效率極其低下,出現例如求一個上千查詢結果在上萬查詢結果的差集,那麼使用not in來查詢的查詢速度極其緩慢,這是必須使用左連結的方式求查詢。 先從簡單的說起,比如在表blog的id情況是這樣的: 在表usert
Python list並集union, 交集intersection, 差集difference
轉載自翔雲並感謝Share python的集合set和其他語言類似,是一個無序不重複元素集, 可用於消除重複元素。 支援union(聯合), intersection(交), difference(差)和sysmmetric difference(對稱差集)等數學運算。 不支援 inde
ES6 運算元組的並集/交集/差集
使用ES6 運算元組 let a = new Set([1, 2, 3]); let b = new Set([3, 5, 2]); // 並集 let unionSet = new Set(
兩個陣列的並集、交集、差集
const arr1 = [1,2,3];const arr2 = [3,4,5];// 並集const union = Array.from(new Set([...arr1,...arr2]));
兩個集合的相等判斷、交集、並集
#include<iostream> using namespace std; const int Size=10; void Get(int a[]); void Judge(int a[],int b[]);//判斷兩函式是否相等 void Intersaction(int a[],
獲取兩個DataTable之間的差集、交集、並集集合(Except、Intersect、Union)
//獲取第一個資料來源DataTable DataTable dt1 = DBHelper.GetDataTable("select top 10 ksdid,user_id,user_pwd from ksd_user_info");
陣列——6、兩個陣列的交集II
給定一個數組,編寫一個函式來計算它們的交集。 示例: 輸入:nums1 = [1, 2, 2, 1], nums2 = [2, 2] 輸出:[2, 2] 說明: 輸出結果中每個元素出現的次數應與元素在兩個陣列中出現的次數一致; 可以不考慮輸出結果的順序。
ES6陣列實現並集、交集、差集
let set1 = new Set(['a','b','c','d','e','f']); let set2 = new Set(['d','e','f','g','h','i']); //並集 let union = [...new Set([...set1,...set2])]; /
陣列取並集、交集、差集
package xuxin; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java