Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*  //from forum - only takes exactly 3 params
function uniteUnique(arr1, arr2, arr3) {
 var newArr;
 //Convert the arguments object into an array
  var args = Array.prototype.slice.call(arguments);
  //Use reduce function to flatten the array
  newArr = args.reduce(function(arrA,arrB){
//console.log(arrA,arrB)
    return arrA.concat(arrB.filter(function(i){
      return arrA.indexOf(i) === -1;  //if element is NOT in 
    }));


  });

   return newArr;                    
}
// */

/*
// Mutates input!!
// one of mine
function uniteUnique(arr, ...others){

  for(let eachArray of others){
    for(let element of eachArray){
      if(arr.indexOf(element) === -1){
        arr.push(element);
      }
    }
  }
  return arr;
}
// */

/*
// Does NOT mutate input
// one of mine
function uniteUnique(arr, ...others){
  const sortedUnion = [...arr]; //shallow copy

  for(let eachArray of others){
    for(let element of eachArray){
      if(sortedUnion.indexOf(element) === -1){
        sortedUnion.push(element);
      }
    }
  }
  return sortedUnion;
}
// */

/*
// one of mine
function uniteUnique(arr) {
  //const sorted = [];  // start empty
  const others = Array.from(arguments).slice(1);

  //loop each subarray
  for (let i = 0; i < others.length; i++) {
    const subArray = others[i];

    //loop each elem
    for (let j = 0; j < subArray.length; j++) { 
      
      if (!arr.includes(subArray[j])) { //push unique values
        arr.push(subArray[j]); 
      }

    }
  }
  return arr;
}
// */

/*
// some other dude's
function uniteUnique(arr) {
let newArr = Array.from(arguments)
let singleArr = newArr.reduce(function(a,b){
  return a.concat(b);
})
console.log(singleArr)

let ans = singleArr.filter(function(a){
   singleArr.indexOf(a) === -1
  console.log("a:", a, singleArr, singleArr.indexOf(a))
})

console.log(ans);

}
// */

/*
//some dudes's using set()
function uniteUnique( ...arr) {
  let sett = new Set();
  arr.forEach(arrays => {
    for (let i = 0; i < arrays.length; i++)
      sett.add(arrays[i]);
  });
  //console.log(sett)
  return Array.from(sett);
}

//uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]);

// */

/*
// interesting map solution
function uniteUnique(arr,...oth) {
  oth.map(item=>{
        return item.map(val=>{
            if(!arr.includes(val)){
            arr.push(val);}
        });
     });
     return arr;
}

// */

//uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
//*
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])) //should return [1, 3, 2, 5, 4].
console.log(uniteUnique([1, 3, 2], [1, [5]], [2, [4]])) //should return [1, 3, 2, [5], [4]].
console.log(uniteUnique([1, 2, 3], [5, 2, 1])) //should return [1, 2, 3, 5].
console.log(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])) //should return [1, 2, 3, 5, 4, 6, 7, 8]


console.log(uniteUnique([1, 2, 3], [5, 2, 2, 2], [2, 1]))


// crap, this fails on all of them
console.log(uniteUnique([1, 2, 3, [5]], [5, [5],[5],[5],[5]]))

// */
node v10.15.2 linux/amd64