Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- intersection([1,2,3], [2,3,4,5])
- [2, 3]
- /* destructively finds the intersection of
- * two arrays in a simple fashion.
- *
- * PARAMS
- * a - first array, must already be sorted
- * b - second array, must already be sorted
- *
- * NOTES
- * State of input arrays is undefined when
- * the function returns. They should be
- * (prolly) be dumped.
- *
- * Should have O(n) operations, where n is
- * n = MIN(a.length, b.length)
- */
- function intersection_destructive(a, b)
- {
- var result = [];
- while( a.length > 0 && b.length > 0 )
- {
- if (a[0] < b[0] ){ a.shift(); }
- else if (a[0] > b[0] ){ b.shift(); }
- else /* they're equal */
- {
- result.push(a.shift());
- b.shift();
- }
- }
- return result;
- }
- /* finds the intersection of
- * two arrays in a simple fashion.
- *
- * PARAMS
- * a - first array, must already be sorted
- * b - second array, must already be sorted
- *
- * NOTES
- *
- * Should have O(n) operations, where n is
- * n = MIN(a.length(), b.length())
- */
- function intersect_safe(a, b)
- {
- var ai=0, bi=0;
- var result = [];
- while( ai < a.length && bi < b.length )
- {
- if (a[ai] < b[bi] ){ ai++; }
- else if (a[ai] > b[bi] ){ bi++; }
- else /* they're equal */
- {
- result.push(a[ai]);
- ai++;
- bi++;
- }
- }
- return result;
- }
- array1.filter(function(n) {
- return array2.indexOf(n) !== -1;
- });
- function intersect(a, b) {
- var setA = new Set(a);
- var setB = new Set(b);
- var intersection = new Set([...setA].filter(x => setB.has(x)));
- return Array.from(intersection);
- }
- function intersect(a, b) {
- return [...new Set(a)].filter(x => new Set(b).has(x));
- }
- var a = [1,2,3];
- var b = [2,3,4,5];
- var c = $(b).not($(b).not(a));
- alert(c);
- _.intersection( [0,345,324] , [1,0,324] ) // gives [0,324]
- function intersect(a, b) {
- var d1 = {};
- var d2 = {};
- var results = [];
- for (var i = 0; i < a.length; i++) {
- d1[a[i]] = true;
- }
- for (var j = 0; j < b.length; j++) {
- d2[b[j]] = true;
- }
- for (var k in d1) {
- if (d2[k])
- results.push(k);
- }
- return results;
- }
- // new version
- function intersect(a, b) {
- var d = {};
- var results = [];
- for (var i = 0; i < b.length; i++) {
- d[b[i]] = true;
- }
- for (var j = 0; j < a.length; j++) {
- if (d[a[j]])
- results.push(a[j]);
- }
- return results;
- }
- function intersect(array1, array2) {
- var result = [];
- // Don't destroy the original arrays
- var a = array1.slice(0);
- var b = array2.slice(0);
- var aLast = a.length - 1;
- var bLast = b.length - 1;
- while (aLast >= 0 && bLast >= 0) {
- if (a[aLast] > b[bLast] ) {
- a.pop();
- aLast--;
- } else if (a[aLast] < b[bLast] ){
- b.pop();
- bLast--;
- } else /* they're equal */ {
- result.push(a.pop());
- b.pop();
- aLast--;
- bLast--;
- }
- }
- return result;
- }
- function intersection(x,y){
- x.sort();y.sort();
- var i=j=0;ret=[];
- while(i<x.length && j<y.length){
- if(x[i]<y[j])i++;
- else if(y[j]<x[i])j++;
- else {
- ret.push(x[i]);
- i++,j++;
- }
- }
- return ret;
- }
- alert(intersection([1,2,3], [2,3,4,5]));
- var arrayContains = Array.prototype.indexOf ?
- function(arr, val) {
- return arr.indexOf(val) > -1;
- } :
- function(arr, val) {
- var i = arr.length;
- while (i--) {
- if (arr[i] === val) {
- return true;
- }
- }
- return false;
- };
- function arrayIntersection() {
- var val, arrayCount, firstArray, i, j, intersection = [], missing;
- var arrays = Array.prototype.slice.call(arguments); // Convert arguments into a real array
- // Search for common values
- firstArray = arrays.pop();
- if (firstArray) {
- j = firstArray.length;
- arrayCount = arrays.length;
- while (j--) {
- val = firstArray[j];
- missing = false;
- // Check val is present in each remaining array
- i = arrayCount;
- while (!missing && i--) {
- if ( !arrayContains(arrays[i], val) ) {
- missing = true;
- }
- }
- if (!missing) {
- intersection.push(val);
- }
- }
- }
- return intersection;
- }
- arrayIntersection( [1, 2, 3, "a"], [1, "a", 2], ["a", 1] ); // Gives [1, "a"];
- function intersect(a, b) {
- var aa = {};
- a.forEach(function(v) { aa[v]=1; });
- return b.filter(function(v) { return v in aa; });
- }
- function intersectIntegers(array1,array2) {
- var seen=[],
- result=[];
- for (var i = 0; i < array1.length; i++) {
- seen[array1[i]] = true;
- }
- for (var i = 0; i < array2.length; i++) {
- if ( seen[array2[i]])
- result.push(array2[i]);
- }
- return result;
- }
- function intersectObjects(array1,array2) {
- var result=[];
- var key="tmpKey_intersect"
- for (var i = 0; i < array1.length; i++) {
- array1[i][key] = true;
- }
- for (var i = 0; i < array2.length; i++) {
- if (array2[i][key])
- result.push(array2[i]);
- }
- for (var i = 0; i < array1.length; i++) {
- delete array1[i][key];
- }
- return result;
- }
- // Calculate intersection of multiple array or object values.
- function intersect (arrList) {
- var arrLength = Object.keys(arrList).length;
- // (Also accepts regular objects as input)
- var index = {};
- for (var i in arrList) {
- for (var j in arrList[i]) {
- var v = arrList[i][j];
- if (index[v] === undefined) index[v] = 0;
- index[v]++;
- };
- };
- var retv = [];
- for (var i in index) {
- if (index[i] == arrLength) retv.push(i);
- };
- return retv;
- };
- intersect ([arr1, arr2, arr3...]);
- intersect ({foo: [1, 2, 3, 4], bar: {a: 2, j:4}}); // [2, 4]
- intersect ([{x: "hello", y: "world"}, ["hello", "user"]]); // ["hello"]
- intersect ([[1, 3, 4, 6, 3], [1, 8, 99]]);
- // Expected: [ '1' ]
- // Actual: [ '1', '3' ]
- if (index[v] === undefined) index[v] = 0;
- index[v]++;
- if (index[v] === undefined) index[v] = {};
- index[v][i] = true; // Mark as present in i input.
- if (index[i] == arrLength) retv.push(i);
- if (Object.keys(index[i]).length == arrLength) retv.push(i);
- // Calculate intersection of multiple array or object values.
- function intersect (arrList) {
- var arrLength = Object.keys(arrList).length;
- // (Also accepts regular objects as input)
- var index = {};
- for (var i in arrList) {
- for (var j in arrList[i]) {
- var v = arrList[i][j];
- if (index[v] === undefined) index[v] = {};
- index[v][i] = true; // Mark as present in i input.
- };
- };
- var retv = [];
- for (var i in index) {
- if (Object.keys(index[i]).length == arrLength) retv.push(i);
- };
- return retv;
- };
- intersect ([[1, 3, 4, 6, 3], [1, 8, 99]]); // [ '1' ]
- function intersection(A,B){
- var result = new Array();
- for (i=0; i<A.length; i++) {
- for (j=0; j<B.length; j++) {
- if (A[i] == B[j] && $.inArray(A[i],result) == -1) {
- result.push(A[i]);
- }
- }
- }
- return result;
- }
- if (!Array.prototype.intersect){
- Array.prototype.intersect = function (arr1) {
- var r = [], o = {}, l = this.length, i, v;
- for (i = 0; i < l; i++) {
- o[this[i]] = true;
- }
- l = arr1.length;
- for (i = 0; i < l; i++) {
- v = arr1[i];
- if (v in o) {
- r.push(v);
- }
- }
- return r;
- };
- }
- function intersection(a,b){
- var rs = [], x = a.length;
- while (x--) b.indexOf(a[x])!=-1 && rs.push(a[x]);
- return rs.sort();
- }
- intersection([1,2,3], [2,3,4,5]);
- //Result: [2,3]
- // Usage
- const intersection = allLists
- .reduce(intersect, allValues)
- .reduce(removeDuplicates, []);
- // Implementation
- const intersect = (intersection, list) =>
- intersection.filter(item =>
- list.some(x => x === item));
- const removeDuplicates = (uniques, item) =>
- uniques.includes(item) ? uniques : uniques.concat(item);
- // Example Data
- const somePeople = [bob, doug, jill];
- const otherPeople = [sarah, bob, jill];
- const morePeople = [jack, jill];
- const allPeople = [...somePeople, ...otherPeople, ...morePeople];
- const allGroups = [somePeople, otherPeople, morePeople];
- // Example Usage
- const intersection = allGroups
- .reduce(intersect, allPeople)
- .reduce(removeDuplicates, []);
- intersection; // [jill]
- _.intersection = function(array) {
- if (array == null) return [];
- var result = [];
- var argsLength = arguments.length;
- for (var i = 0, length = array.length; i < length; i++) {
- var item = array[i];
- if (_.contains(result, item)) continue;
- for (var j = 1; j < argsLength; j++) {
- if (!_.contains(arguments[j], item)) break;
- }
- if (j === argsLength) result.push(item);
- }
- return result;
- };
- Array.prototype.contains = function(elem) {
- return(this.indexOf(elem) > -1);
- };
- Array.prototype.intersect = function( array ) {
- // this is naive--could use some optimization
- var result = [];
- for ( var i = 0; i < this.length; i++ ) {
- if ( array.contains(this[i]) && !result.contains(this[i]) )
- result.push( this[i] );
- }
- return result;
- }
- getIntersection: (arrays) ->
- if not arrays.length
- return []
- a1 = arrays[0]
- for a2 in arrays.slice(1)
- a = (val for val in a1 when val in a2)
- a1 = a
- return a1.unique()
- function intersect() {
- const last = arguments.length - 1;
- var seen={};
- var result=[];
- for (var i = 0; i < last; i++) {
- for (var j = 0; j < arguments[i].length; j++) {
- if (seen[arguments[i][j]]) {
- seen[arguments[i][j]] += 1;
- }
- else if (!i) {
- seen[arguments[i][j]] = 1;
- }
- }
- }
- for (var i = 0; i < arguments[last].length; i++) {
- if ( seen[arguments[last][i]] === last)
- result.push(arguments[last][i]);
- }
- return result;
- }
- var listA = [1,2,3,4,5,6,7];
- var listB = [2,4,6,8];
- var result = listA.filter(itemA=> {
- return listB.some(itemB => itemB === itemA);
- });
- function arrayIntersect(arrayOfArrays)
- {
- var arrayCopy = arrayOfArrays.slice(),
- baseArray = arrayCopy.pop();
- return baseArray.filter(function(item) {
- return arrayCopy.every(function(itemList) {
- return itemList.indexOf(item) !== -1;
- });
- });
- }
- function diffArray(arr1, arr2) {
- var newArr = [];
- var large = arr1.length>=arr2.length?arr1:arr2;
- var small = JSON.stringify(large) == JSON.stringify(arr1)?arr2:arr1;
- for(var i=0;i<large.length;i++){
- var copyExists = false;
- for(var j =0;j<small.length;j++){
- if(large[i]==small[j]){
- copyExists= true;
- break;
- }
- }
- if(!copyExists)
- {
- newArr.push(large[i]);
- }
- }
- for(var i=0;i<small.length;i++){
- var copyExists = false;
- for(var j =0;j<large.length;j++){
- if(large[j]==small[i]){
- copyExists= true;
- break;
- }
- }
- if(!copyExists)
- {
- newArr.push(small[i]);
- }
- }
- return newArr;
- }
- function intersection (a, b) {
- var seen = a.reduce(function (h, k) {
- h[k] = true;
- return h;
- }, {});
- return b.filter(function (k) {
- var exists = seen[k];
- delete seen[k];
- return exists;
- });
- }
- // process array [element, element...], if allow abort ignore the result
- function processArray(arr_a, cb_a, blnAllowAbort_a)
- {
- var arrResult = [];
- var blnAborted = false;
- var intI = 0;
- while ((intI < arr_a.length) && (blnAborted === false))
- {
- if (blnAllowAbort_a)
- {
- blnAborted = cb_a(arr_a[intI]);
- }
- else
- {
- arrResult[intI] = cb_a(arr_a[intI]);
- }
- intI++;
- }
- return arrResult;
- }
- // process array of operations [operation,arguments...]
- function processOperations(arrOperations_a)
- {
- var arrResult = [];
- var fnOperationE;
- for(var intI = 0, intR = 0; intI < arrOperations_a.length; intI+=2, intR++)
- {
- var fnOperation = arrOperations_a[intI+0];
- var fnArgs = arrOperations_a[intI+1];
- if (fnArgs === undefined)
- {
- arrResult[intR] = fnOperation();
- }
- else
- {
- arrResult[intR] = fnOperation(fnArgs);
- }
- }
- return arrResult;
- }
- // return whether an element exists in an array
- function find(arr_a, varElement_a)
- {
- var blnResult = false;
- processArray(arr_a, function(varToMatch_a)
- {
- var blnAbort = false;
- if (varToMatch_a === varElement_a)
- {
- blnResult = true;
- blnAbort = true;
- }
- return blnAbort;
- }, true);
- return blnResult;
- }
- // return the union of all sets
- function union(arr_a)
- {
- var arrResult = [];
- var intI = 0;
- processArray(arr_a, function(arrSet_a)
- {
- processArray(arrSet_a, function(varElement_a)
- {
- // if the element doesn't exist in our result
- if (find(arrResult, varElement_a) === false)
- {
- // add it
- arrResult[intI] = varElement_a;
- intI++;
- }
- });
- });
- return arrResult;
- }
- // return the intersection of all sets
- function intersection(arr_a)
- {
- var arrResult = [];
- var intI = 0;
- // for each set
- processArray(arr_a, function(arrSet_a)
- {
- // every number is a candidate
- processArray(arrSet_a, function(varCandidate_a)
- {
- var blnCandidate = true;
- // for each set
- processArray(arr_a, function(arrSet_a)
- {
- // check that the candidate exists
- var blnFoundPart = find(arrSet_a, varCandidate_a);
- // if the candidate does not exist
- if (blnFoundPart === false)
- {
- // no longer a candidate
- blnCandidate = false;
- }
- });
- if (blnCandidate)
- {
- // if the candidate doesn't exist in our result
- if (find(arrResult, varCandidate_a) === false)
- {
- // add it
- arrResult[intI] = varCandidate_a;
- intI++;
- }
- }
- });
- });
- return arrResult;
- }
- var strOutput = ''
- var arrSet1 = [1,2,3];
- var arrSet2 = [2,5,6];
- var arrSet3 = [7,8,9,2];
- // return the union of the sets
- strOutput = union([arrSet1, arrSet2, arrSet3]);
- alert(strOutput);
- // return the intersection of 3 sets
- strOutput = intersection([arrSet1, arrSet2, arrSet3]);
- alert(strOutput);
- // of 3 sets of sets, which set is the intersecting set
- strOutput = processOperations([intersection,[[arrSet1, arrSet2], [arrSet2], [arrSet2, arrSet3]]]);
- alert(strOutput);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement