I'm trying to solve a freeCodeCamp exercise with this goal:
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
So what I do is concatenate all the arguments into a single array called everything
. I then search the array for duplicates, then search the arguments for these duplicates and .splice()
them out.
So far everything works as expected, but the last number of the last argument does not get removed and I can't really figure out why.
Can anybody please point out what I'm doing wrong? Please keep in mind that I'm trying to learn, so obvious things probably won't be obvious to me and need to be pointed out. Thanks in advance.
function unite(arr1, arr2, arr3) { var everything = []; //concat all arrays except the first one for(var x = 0; x < arguments.length; x++) { for(var y = 0; y < arguments[x].length; y++) { everything.push(arguments[x][y]); } } //function that returns duplicates function returnUnique(arr) { return arr.reduce(function(dupes, val, i) { if (arr.indexOf(val) !== i && dupes.indexOf(val) === -1) { dupes.push(val); } return dupes; }, []); } //return duplicates var dupes = returnUnique(everything); //remove duplicates from all arguments except the first one for(var n = 1; n < arguments.length; n++) { for(var m = 0; m < dupes.length; m++) { if(arguments[n].hasOwnProperty(dupes[m])) { arguments[n].splice(arguments[n].indexOf(dupes[m]), 1); } } } //return concatenation of the reduced arguments return arr1.concat(arr2).concat(arr3); } //this returns [1, 3, 2, 5, 4, 2] unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Looks like you overcomplicated it a bit ;)
function unite() { return [].concat.apply([], arguments).filter(function(elem, index, self) { return self.indexOf(elem) === index; }); } res = unite([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]); document.write('<pre>'+JSON.stringify(res));
We split the problem into two steps:
This part handles the first step:
[].concat.apply([], arguments)
The built-in method someArray.concat(array1, array2 etc)
appends given arrays to the target. For example,
[1,2,3].concat([4,5],[6],[7,8]) == [1,2,3,4,5,6,7,8]
If our function had fixed arguments, we could call concat
directly:
function unite(array1, array2, array3) {
var combined = [].concat(array1, array2, array3);
// or
var combined = array1.concat(array2, array3);
but as we don't know how many args we're going to receive, we have to use apply
.
someFunction.apply(thisObject, [arg1, arg2, etc])
is the same as
thisObject.someFunction(arg1, arg2, etc)
so the above line
var combined = [].concat(array1, array2, array3);
can be written as
var combined = concat.apply([], [array1, array2, array3]);
or simply
var combined = concat.apply([], arguments);
where arguments
is a special array-like object that contains all function arguments (actual parameters).
Actually, last two lines are not going to work, because concat
isn't a plain function, it's a method of Array
objects and therefore a member of Array.prototype
structure. We have to tell the JS engine where to find concat
. We can use Array.prototype
directly:
var combined = Array.prototype.concat.apply([], arguments);
or create a new, unrelated, array object and pull concat
from there:
var combined = [].concat.apply([], arguments);
This prototype method is slightly more efficient (since we're not creating a dummy object), but also more verbose.
Anyways, the first step is now complete. To eliminate duplicates, we use the following method:
combined.filter(function(elem, index) {
return combined.indexOf(elem) === index;
})
For explanations and alternatives see this post .
Finally, we get rid of the temporary variable ( combined
) and chain "combine" and "dedupe" calls together:
return [].concat.apply([], arguments).filter(function(elem, index, self) {
return self.indexOf(elem) === index;
});
using the 3rd argument ("this array") of filter
because we don't have a variable anymore.
Simple, isn't it? ;) Let us know if you have questions.
Finally, a small exercise if you're interested:
Write
combine
anddedupe
as separate functions. Create a functioncompose
that takes two functionsa
andb
and returns a new function that runs these functions in reverse order, so thatcompose(a,b)(argument)
will be the same asb(a(argument))
. Replace the above definition ofunite
withunite = compose(combine, dedupe)
and make sure it works exactly the same.
You can also try this :
var Data = [[1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]] var UniqueValues = [] for (var i = 0; i < Data.length; i++) { UniqueValues = [...new Set(UniqueValues.concat(Data[i]))] } console.log(UniqueValues)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.