Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. //finds the symmetric difference between a series of arrays
  12.  
  13. "use strict";
  14.  
  15. function symDiff() {
  16. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  17. args[_key] = arguments[_key];
  18. }
  19.  
  20. //loop through arrays
  21. return args.reduce(function (symDiff, arr, i) {
  22. //dedupe arr
  23. //use object keys to dedupe
  24. var dedupeObj = arr.reduce(function (obj, num) {
  25. obj[num] = "I can't get no";
  26. return obj;
  27. }, {});
  28. //reform array from obj keys
  29. arr = Object.keys(dedupeObj).map(function (key) {
  30. return parseInt(key);
  31. });
  32. //merge two arrays
  33. symDiff = symDiff.concat(arr);
  34. //count instances of values by using obj keys to dedupe
  35. var countObj = symDiff.reduce(function (obj, num) {
  36. obj[num] = ++obj[num] || 1;
  37. return obj;
  38. }, {});
  39. console.log({ countObj: countObj });
  40. //convert keys that have a value of 1 into array
  41. var uniques = [];
  42. for (key in countObj) {
  43. if (countObj[key] === 1) {
  44. uniques.push(parseInt(key));
  45. }
  46. }
  47. return uniques;
  48. }, []);
  49. }
  50.  
  51. var result = symDiff([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);
  52. console.log(result);
  53.  
  54. //[2, 3, 4, 6, 7]
  55. </script>
  56.  
  57.  
  58.  
  59. <script id="jsbin-source-javascript" type="text/javascript">//finds the symmetric difference between a series of arrays
  60.  
  61. function symDiff(...args) {
  62. //loop through arrays
  63. return args.reduce( (symDiff , arr, i) => {
  64. //dedupe arr
  65. //use object keys to dedupe
  66. const dedupeObj = arr.reduce( (obj, num) => {
  67. obj[num] = "I can't get no";
  68. return obj;
  69. }, {});
  70. //reform array from obj keys
  71. arr = Object.keys(dedupeObj).map( (key) => parseInt(key));
  72. //merge two arrays
  73. symDiff = symDiff.concat(arr);
  74. //count instances of values by using obj keys to dedupe
  75. const countObj = symDiff.reduce( (obj, num) => {
  76. obj[num] = ++obj[num] || 1;
  77. return obj;
  78. }, {});
  79. console.log({countObj});
  80. //convert keys that have a value of 1 into array
  81. const uniques = [];
  82. for (key in countObj) {
  83. if (countObj[key] === 1) {uniques.push(parseInt(key));}
  84. }
  85. return uniques;
  86. }, []);
  87. }
  88.  
  89. const result = symDiff([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);
  90. console.log(result);
  91.  
  92.  
  93. //[2, 3, 4, 6, 7]</script></body>
  94. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement