💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
``` // +---------------------------------------------------------------------- /** a = [1,2,3,4,5]; b = [3,4,5,6,7]; c = array_intersection(a, b); //交集 d = array_union(a, b); //并集 e = array_difference(a, b); //差集 f = array_difference(b, a); g = array_difference(d,c) console.log("test array a:", a, " b:", b); console.log("a & b :", c); console.log("a + b :", d); console.log("a - b:", e); console.log("b - a:", f); console.log("d - c:", g); */ function array_remove_repeat(a) { // 去重 var r = []; for(var i = 0; i < a.length; i ++) { var flag = true; var temp = a[i]; for(var j = 0; j < r.length; j ++) { if(temp === r[j]) { flag = false; break; } } if(flag) { r.push(temp); } } return r; } function array_intersection(a, b) { // 交集 var result = []; for(var i = 0; i < b.length; i ++) { var temp = b[i]; for(var j = 0; j < a.length; j ++) { if(temp === a[j]) { result.push(temp); break; } } } return array_remove_repeat(result); } function array_union(a, b) { // 并集 return array_remove_repeat(a.concat(b)); } function array_difference(a, b) { // 差集 a - b //clone = a var clone = a.slice(0); for(var i = 0; i < b.length; i ++) { var temp = b[i]; for(var j = 0; j < clone.length; j ++) { if(temp === clone[j]) { //remove clone[j] clone.splice(j,1); } } } return array_remove_repeat(clone); } // +---------------------------------------------------------------------- // | 去重、交集、并集、差集 end // +---------------------------------------------------------------------- ```