SHARE
TWEET

Untitled

a guest Jul 18th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```javascript
  2. function reduce(array, callback, initialValue) {
  3.   let accumulator = initialValue;
  4.   for (let i = 0; i < array.length; i++) {
  5.     accumulator = callback(accumulator, array[i]);
  6.   }
  7.   return accumulator;
  8. }
  9.  
  10. function addNonDuplicates(array1, array2) {
  11.   let output = array1;
  12.   for (let i = 0; i < array2.length; i++) {
  13.     // if ith element is not in output, push it to output
  14.     if (!output.includes(array2[i])) {
  15.       output.push(array2[i]);
  16.     }
  17.   }
  18.   return output;
  19. }
  20.  
  21. function union(arrayOfArrays) {
  22.   let newArray = [];
  23.   newArray = reduce(arrayOfArrays, addNonDuplicates, []);
  24.   return newArray
  25. }
  26.  
  27.  
  28. // Uncomment these to check your work!
  29. const arr1 = [5, 10, 15];
  30. const arr2 = [15, 88, 1, 5, 7];
  31. const arr3 = [100, 15, 10, 1, 5];
  32. console.log(union([arr1, arr2, arr3])); // should log: [5, 10, 15, 88, 1, 7, 100]
  33. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top