Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.71 KB | None | 0 0
  1. intersection([1,2,3], [2,3,4,5])
  2.  
  3. [2, 3]
  4.  
  5. /* destructively finds the intersection of
  6. * two arrays in a simple fashion.
  7. *
  8. * PARAMS
  9. * a - first array, must already be sorted
  10. * b - second array, must already be sorted
  11. *
  12. * NOTES
  13. * State of input arrays is undefined when
  14. * the function returns. They should be
  15. * (prolly) be dumped.
  16. *
  17. * Should have O(n) operations, where n is
  18. * n = MIN(a.length, b.length)
  19. */
  20. function intersection_destructive(a, b)
  21. {
  22. var result = [];
  23. while( a.length > 0 && b.length > 0 )
  24. {
  25. if (a[0] < b[0] ){ a.shift(); }
  26. else if (a[0] > b[0] ){ b.shift(); }
  27. else /* they're equal */
  28. {
  29. result.push(a.shift());
  30. b.shift();
  31. }
  32. }
  33.  
  34. return result;
  35. }
  36.  
  37. /* finds the intersection of
  38. * two arrays in a simple fashion.
  39. *
  40. * PARAMS
  41. * a - first array, must already be sorted
  42. * b - second array, must already be sorted
  43. *
  44. * NOTES
  45. *
  46. * Should have O(n) operations, where n is
  47. * n = MIN(a.length(), b.length())
  48. */
  49. function intersect_safe(a, b)
  50. {
  51. var ai=0, bi=0;
  52. var result = [];
  53.  
  54. while( ai < a.length && bi < b.length )
  55. {
  56. if (a[ai] < b[bi] ){ ai++; }
  57. else if (a[ai] > b[bi] ){ bi++; }
  58. else /* they're equal */
  59. {
  60. result.push(a[ai]);
  61. ai++;
  62. bi++;
  63. }
  64. }
  65.  
  66. return result;
  67. }
  68.  
  69. array1.filter(function(n) {
  70. return array2.indexOf(n) !== -1;
  71. });
  72.  
  73. function intersect(a, b) {
  74. var setA = new Set(a);
  75. var setB = new Set(b);
  76. var intersection = new Set([...setA].filter(x => setB.has(x)));
  77. return Array.from(intersection);
  78. }
  79.  
  80. function intersect(a, b) {
  81. return [...new Set(a)].filter(x => new Set(b).has(x));
  82. }
  83.  
  84. var a = [1,2,3];
  85. var b = [2,3,4,5];
  86. var c = $(b).not($(b).not(a));
  87. alert(c);
  88.  
  89. _.intersection( [0,345,324] , [1,0,324] ) // gives [0,324]
  90.  
  91. function intersect(a, b) {
  92. var d1 = {};
  93. var d2 = {};
  94. var results = [];
  95. for (var i = 0; i < a.length; i++) {
  96. d1[a[i]] = true;
  97. }
  98. for (var j = 0; j < b.length; j++) {
  99. d2[b[j]] = true;
  100. }
  101. for (var k in d1) {
  102. if (d2[k])
  103. results.push(k);
  104. }
  105. return results;
  106. }
  107.  
  108. // new version
  109. function intersect(a, b) {
  110. var d = {};
  111. var results = [];
  112. for (var i = 0; i < b.length; i++) {
  113. d[b[i]] = true;
  114. }
  115. for (var j = 0; j < a.length; j++) {
  116. if (d[a[j]])
  117. results.push(a[j]);
  118. }
  119. return results;
  120. }
  121.  
  122. function intersect(array1, array2) {
  123. var result = [];
  124. // Don't destroy the original arrays
  125. var a = array1.slice(0);
  126. var b = array2.slice(0);
  127. var aLast = a.length - 1;
  128. var bLast = b.length - 1;
  129. while (aLast >= 0 && bLast >= 0) {
  130. if (a[aLast] > b[bLast] ) {
  131. a.pop();
  132. aLast--;
  133. } else if (a[aLast] < b[bLast] ){
  134. b.pop();
  135. bLast--;
  136. } else /* they're equal */ {
  137. result.push(a.pop());
  138. b.pop();
  139. aLast--;
  140. bLast--;
  141. }
  142. }
  143. return result;
  144. }
  145.  
  146. function intersection(x,y){
  147. x.sort();y.sort();
  148. var i=j=0;ret=[];
  149. while(i<x.length && j<y.length){
  150. if(x[i]<y[j])i++;
  151. else if(y[j]<x[i])j++;
  152. else {
  153. ret.push(x[i]);
  154. i++,j++;
  155. }
  156. }
  157. return ret;
  158. }
  159.  
  160. alert(intersection([1,2,3], [2,3,4,5]));
  161.  
  162. var arrayContains = Array.prototype.indexOf ?
  163. function(arr, val) {
  164. return arr.indexOf(val) > -1;
  165. } :
  166. function(arr, val) {
  167. var i = arr.length;
  168. while (i--) {
  169. if (arr[i] === val) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. };
  175.  
  176. function arrayIntersection() {
  177. var val, arrayCount, firstArray, i, j, intersection = [], missing;
  178. var arrays = Array.prototype.slice.call(arguments); // Convert arguments into a real array
  179.  
  180. // Search for common values
  181. firstArray = arrays.pop();
  182. if (firstArray) {
  183. j = firstArray.length;
  184. arrayCount = arrays.length;
  185. while (j--) {
  186. val = firstArray[j];
  187. missing = false;
  188.  
  189. // Check val is present in each remaining array
  190. i = arrayCount;
  191. while (!missing && i--) {
  192. if ( !arrayContains(arrays[i], val) ) {
  193. missing = true;
  194. }
  195. }
  196. if (!missing) {
  197. intersection.push(val);
  198. }
  199. }
  200. }
  201. return intersection;
  202. }
  203.  
  204. arrayIntersection( [1, 2, 3, "a"], [1, "a", 2], ["a", 1] ); // Gives [1, "a"];
  205.  
  206. function intersect(a, b) {
  207. var aa = {};
  208. a.forEach(function(v) { aa[v]=1; });
  209. return b.filter(function(v) { return v in aa; });
  210. }
  211.  
  212. function intersectIntegers(array1,array2) {
  213. var seen=[],
  214. result=[];
  215. for (var i = 0; i < array1.length; i++) {
  216. seen[array1[i]] = true;
  217. }
  218. for (var i = 0; i < array2.length; i++) {
  219. if ( seen[array2[i]])
  220. result.push(array2[i]);
  221. }
  222. return result;
  223. }
  224.  
  225. function intersectObjects(array1,array2) {
  226. var result=[];
  227. var key="tmpKey_intersect"
  228. for (var i = 0; i < array1.length; i++) {
  229. array1[i][key] = true;
  230. }
  231. for (var i = 0; i < array2.length; i++) {
  232. if (array2[i][key])
  233. result.push(array2[i]);
  234. }
  235. for (var i = 0; i < array1.length; i++) {
  236. delete array1[i][key];
  237. }
  238. return result;
  239. }
  240.  
  241. // Calculate intersection of multiple array or object values.
  242. function intersect (arrList) {
  243. var arrLength = Object.keys(arrList).length;
  244. // (Also accepts regular objects as input)
  245. var index = {};
  246. for (var i in arrList) {
  247. for (var j in arrList[i]) {
  248. var v = arrList[i][j];
  249. if (index[v] === undefined) index[v] = 0;
  250. index[v]++;
  251. };
  252. };
  253. var retv = [];
  254. for (var i in index) {
  255. if (index[i] == arrLength) retv.push(i);
  256. };
  257. return retv;
  258. };
  259.  
  260. intersect ([arr1, arr2, arr3...]);
  261.  
  262. intersect ({foo: [1, 2, 3, 4], bar: {a: 2, j:4}}); // [2, 4]
  263. intersect ([{x: "hello", y: "world"}, ["hello", "user"]]); // ["hello"]
  264.  
  265. intersect ([[1, 3, 4, 6, 3], [1, 8, 99]]);
  266. // Expected: [ '1' ]
  267. // Actual: [ '1', '3' ]
  268.  
  269. if (index[v] === undefined) index[v] = 0;
  270. index[v]++;
  271.  
  272. if (index[v] === undefined) index[v] = {};
  273. index[v][i] = true; // Mark as present in i input.
  274.  
  275. if (index[i] == arrLength) retv.push(i);
  276.  
  277. if (Object.keys(index[i]).length == arrLength) retv.push(i);
  278.  
  279. // Calculate intersection of multiple array or object values.
  280. function intersect (arrList) {
  281. var arrLength = Object.keys(arrList).length;
  282. // (Also accepts regular objects as input)
  283. var index = {};
  284. for (var i in arrList) {
  285. for (var j in arrList[i]) {
  286. var v = arrList[i][j];
  287. if (index[v] === undefined) index[v] = {};
  288. index[v][i] = true; // Mark as present in i input.
  289. };
  290. };
  291. var retv = [];
  292. for (var i in index) {
  293. if (Object.keys(index[i]).length == arrLength) retv.push(i);
  294. };
  295. return retv;
  296. };
  297.  
  298. intersect ([[1, 3, 4, 6, 3], [1, 8, 99]]); // [ '1' ]
  299.  
  300. function intersection(A,B){
  301. var result = new Array();
  302. for (i=0; i<A.length; i++) {
  303. for (j=0; j<B.length; j++) {
  304. if (A[i] == B[j] && $.inArray(A[i],result) == -1) {
  305. result.push(A[i]);
  306. }
  307. }
  308. }
  309. return result;
  310. }
  311.  
  312. if (!Array.prototype.intersect){
  313. Array.prototype.intersect = function (arr1) {
  314.  
  315. var r = [], o = {}, l = this.length, i, v;
  316. for (i = 0; i < l; i++) {
  317. o[this[i]] = true;
  318. }
  319. l = arr1.length;
  320. for (i = 0; i < l; i++) {
  321. v = arr1[i];
  322. if (v in o) {
  323. r.push(v);
  324. }
  325. }
  326. return r;
  327. };
  328. }
  329.  
  330. function intersection(a,b){
  331. var rs = [], x = a.length;
  332. while (x--) b.indexOf(a[x])!=-1 && rs.push(a[x]);
  333. return rs.sort();
  334. }
  335.  
  336. intersection([1,2,3], [2,3,4,5]);
  337. //Result: [2,3]
  338.  
  339. // Usage
  340. const intersection = allLists
  341. .reduce(intersect, allValues)
  342. .reduce(removeDuplicates, []);
  343.  
  344.  
  345. // Implementation
  346. const intersect = (intersection, list) =>
  347. intersection.filter(item =>
  348. list.some(x => x === item));
  349.  
  350. const removeDuplicates = (uniques, item) =>
  351. uniques.includes(item) ? uniques : uniques.concat(item);
  352.  
  353.  
  354. // Example Data
  355. const somePeople = [bob, doug, jill];
  356. const otherPeople = [sarah, bob, jill];
  357. const morePeople = [jack, jill];
  358.  
  359. const allPeople = [...somePeople, ...otherPeople, ...morePeople];
  360. const allGroups = [somePeople, otherPeople, morePeople];
  361.  
  362. // Example Usage
  363. const intersection = allGroups
  364. .reduce(intersect, allPeople)
  365. .reduce(removeDuplicates, []);
  366.  
  367. intersection; // [jill]
  368.  
  369. _.intersection = function(array) {
  370. if (array == null) return [];
  371. var result = [];
  372. var argsLength = arguments.length;
  373. for (var i = 0, length = array.length; i < length; i++) {
  374. var item = array[i];
  375. if (_.contains(result, item)) continue;
  376. for (var j = 1; j < argsLength; j++) {
  377. if (!_.contains(arguments[j], item)) break;
  378. }
  379. if (j === argsLength) result.push(item);
  380. }
  381. return result;
  382. };
  383.  
  384. Array.prototype.contains = function(elem) {
  385. return(this.indexOf(elem) > -1);
  386. };
  387.  
  388. Array.prototype.intersect = function( array ) {
  389. // this is naive--could use some optimization
  390. var result = [];
  391. for ( var i = 0; i < this.length; i++ ) {
  392. if ( array.contains(this[i]) && !result.contains(this[i]) )
  393. result.push( this[i] );
  394. }
  395. return result;
  396. }
  397.  
  398. getIntersection: (arrays) ->
  399. if not arrays.length
  400. return []
  401. a1 = arrays[0]
  402. for a2 in arrays.slice(1)
  403. a = (val for val in a1 when val in a2)
  404. a1 = a
  405. return a1.unique()
  406.  
  407. function intersect() {
  408. const last = arguments.length - 1;
  409. var seen={};
  410. var result=[];
  411. for (var i = 0; i < last; i++) {
  412. for (var j = 0; j < arguments[i].length; j++) {
  413. if (seen[arguments[i][j]]) {
  414. seen[arguments[i][j]] += 1;
  415. }
  416. else if (!i) {
  417. seen[arguments[i][j]] = 1;
  418. }
  419. }
  420. }
  421. for (var i = 0; i < arguments[last].length; i++) {
  422. if ( seen[arguments[last][i]] === last)
  423. result.push(arguments[last][i]);
  424. }
  425. return result;
  426. }
  427.  
  428. var listA = [1,2,3,4,5,6,7];
  429. var listB = [2,4,6,8];
  430.  
  431. var result = listA.filter(itemA=> {
  432. return listB.some(itemB => itemB === itemA);
  433. });
  434.  
  435. function arrayIntersect(arrayOfArrays)
  436. {
  437. var arrayCopy = arrayOfArrays.slice(),
  438. baseArray = arrayCopy.pop();
  439.  
  440. return baseArray.filter(function(item) {
  441. return arrayCopy.every(function(itemList) {
  442. return itemList.indexOf(item) !== -1;
  443. });
  444. });
  445. }
  446.  
  447. function diffArray(arr1, arr2) {
  448. var newArr = [];
  449.  
  450. var large = arr1.length>=arr2.length?arr1:arr2;
  451. var small = JSON.stringify(large) == JSON.stringify(arr1)?arr2:arr1;
  452. for(var i=0;i<large.length;i++){
  453. var copyExists = false;
  454. for(var j =0;j<small.length;j++){
  455. if(large[i]==small[j]){
  456. copyExists= true;
  457. break;
  458. }
  459. }
  460. if(!copyExists)
  461. {
  462. newArr.push(large[i]);
  463. }
  464. }
  465.  
  466. for(var i=0;i<small.length;i++){
  467. var copyExists = false;
  468. for(var j =0;j<large.length;j++){
  469. if(large[j]==small[i]){
  470. copyExists= true;
  471. break;
  472. }
  473. }
  474. if(!copyExists)
  475. {
  476. newArr.push(small[i]);
  477. }
  478. }
  479.  
  480.  
  481. return newArr;
  482. }
  483.  
  484. function intersection (a, b) {
  485. var seen = a.reduce(function (h, k) {
  486. h[k] = true;
  487. return h;
  488. }, {});
  489.  
  490. return b.filter(function (k) {
  491. var exists = seen[k];
  492. delete seen[k];
  493. return exists;
  494. });
  495. }
  496.  
  497. // process array [element, element...], if allow abort ignore the result
  498. function processArray(arr_a, cb_a, blnAllowAbort_a)
  499. {
  500. var arrResult = [];
  501. var blnAborted = false;
  502. var intI = 0;
  503.  
  504. while ((intI < arr_a.length) && (blnAborted === false))
  505. {
  506. if (blnAllowAbort_a)
  507. {
  508. blnAborted = cb_a(arr_a[intI]);
  509. }
  510. else
  511. {
  512. arrResult[intI] = cb_a(arr_a[intI]);
  513. }
  514. intI++;
  515. }
  516.  
  517. return arrResult;
  518. }
  519.  
  520. // process array of operations [operation,arguments...]
  521. function processOperations(arrOperations_a)
  522. {
  523. var arrResult = [];
  524. var fnOperationE;
  525.  
  526. for(var intI = 0, intR = 0; intI < arrOperations_a.length; intI+=2, intR++)
  527. {
  528. var fnOperation = arrOperations_a[intI+0];
  529. var fnArgs = arrOperations_a[intI+1];
  530. if (fnArgs === undefined)
  531. {
  532. arrResult[intR] = fnOperation();
  533. }
  534. else
  535. {
  536. arrResult[intR] = fnOperation(fnArgs);
  537. }
  538. }
  539.  
  540. return arrResult;
  541. }
  542.  
  543. // return whether an element exists in an array
  544. function find(arr_a, varElement_a)
  545. {
  546. var blnResult = false;
  547.  
  548. processArray(arr_a, function(varToMatch_a)
  549. {
  550. var blnAbort = false;
  551.  
  552. if (varToMatch_a === varElement_a)
  553. {
  554. blnResult = true;
  555. blnAbort = true;
  556. }
  557.  
  558. return blnAbort;
  559. }, true);
  560.  
  561. return blnResult;
  562. }
  563.  
  564. // return the union of all sets
  565. function union(arr_a)
  566. {
  567. var arrResult = [];
  568. var intI = 0;
  569.  
  570. processArray(arr_a, function(arrSet_a)
  571. {
  572. processArray(arrSet_a, function(varElement_a)
  573. {
  574. // if the element doesn't exist in our result
  575. if (find(arrResult, varElement_a) === false)
  576. {
  577. // add it
  578. arrResult[intI] = varElement_a;
  579. intI++;
  580. }
  581. });
  582. });
  583.  
  584. return arrResult;
  585. }
  586.  
  587. // return the intersection of all sets
  588. function intersection(arr_a)
  589. {
  590. var arrResult = [];
  591. var intI = 0;
  592.  
  593. // for each set
  594. processArray(arr_a, function(arrSet_a)
  595. {
  596. // every number is a candidate
  597. processArray(arrSet_a, function(varCandidate_a)
  598. {
  599. var blnCandidate = true;
  600.  
  601. // for each set
  602. processArray(arr_a, function(arrSet_a)
  603. {
  604. // check that the candidate exists
  605. var blnFoundPart = find(arrSet_a, varCandidate_a);
  606.  
  607. // if the candidate does not exist
  608. if (blnFoundPart === false)
  609. {
  610. // no longer a candidate
  611. blnCandidate = false;
  612. }
  613. });
  614.  
  615. if (blnCandidate)
  616. {
  617. // if the candidate doesn't exist in our result
  618. if (find(arrResult, varCandidate_a) === false)
  619. {
  620. // add it
  621. arrResult[intI] = varCandidate_a;
  622. intI++;
  623. }
  624. }
  625. });
  626. });
  627.  
  628. return arrResult;
  629. }
  630.  
  631. var strOutput = ''
  632.  
  633. var arrSet1 = [1,2,3];
  634. var arrSet2 = [2,5,6];
  635. var arrSet3 = [7,8,9,2];
  636.  
  637. // return the union of the sets
  638. strOutput = union([arrSet1, arrSet2, arrSet3]);
  639. alert(strOutput);
  640.  
  641. // return the intersection of 3 sets
  642. strOutput = intersection([arrSet1, arrSet2, arrSet3]);
  643. alert(strOutput);
  644.  
  645. // of 3 sets of sets, which set is the intersecting set
  646. strOutput = processOperations([intersection,[[arrSet1, arrSet2], [arrSet2], [arrSet2, arrSet3]]]);
  647. alert(strOutput);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement