Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- //finds the symmetric difference between a series of arrays
- "use strict";
- function symDiff() {
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- //loop through arrays
- return args.reduce(function (symDiff, arr, i) {
- //dedupe arr
- //use object keys to dedupe
- var dedupeObj = arr.reduce(function (obj, num) {
- obj[num] = "I can't get no";
- return obj;
- }, {});
- //reform array from obj keys
- arr = Object.keys(dedupeObj).map(function (key) {
- return parseInt(key);
- });
- //merge two arrays
- symDiff = symDiff.concat(arr);
- //count instances of values by using obj keys to dedupe
- var countObj = symDiff.reduce(function (obj, num) {
- obj[num] = ++obj[num] || 1;
- return obj;
- }, {});
- console.log({ countObj: countObj });
- //convert keys that have a value of 1 into array
- var uniques = [];
- for (key in countObj) {
- if (countObj[key] === 1) {
- uniques.push(parseInt(key));
- }
- }
- return uniques;
- }, []);
- }
- var result = symDiff([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);
- console.log(result);
- //[2, 3, 4, 6, 7]
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">//finds the symmetric difference between a series of arrays
- function symDiff(...args) {
- //loop through arrays
- return args.reduce( (symDiff , arr, i) => {
- //dedupe arr
- //use object keys to dedupe
- const dedupeObj = arr.reduce( (obj, num) => {
- obj[num] = "I can't get no";
- return obj;
- }, {});
- //reform array from obj keys
- arr = Object.keys(dedupeObj).map( (key) => parseInt(key));
- //merge two arrays
- symDiff = symDiff.concat(arr);
- //count instances of values by using obj keys to dedupe
- const countObj = symDiff.reduce( (obj, num) => {
- obj[num] = ++obj[num] || 1;
- return obj;
- }, {});
- console.log({countObj});
- //convert keys that have a value of 1 into array
- const uniques = [];
- for (key in countObj) {
- if (countObj[key] === 1) {uniques.push(parseInt(key));}
- }
- return uniques;
- }, []);
- }
- const result = symDiff([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);
- console.log(result);
- //[2, 3, 4, 6, 7]</script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement