Sorted Union question

Tell us what’s happening:
I almost figured it out but the split on line 31 doesn’t work. Can anybody tell me why?

Your code so far

function uniteUnique(arr) {
  var args = [];
  let counter=0;
  for (let i=0; i<arguments.length;i++) {
    args[i] = arguments[i];
console.log("args is " +JSON.stringify(args));
var merged = [].concat.apply([], args);
console.log("merged is " + JSON.stringify(merged));
var mergedCopy = JSON.parse(JSON.stringify(merged));
console.log("mergedCopy is " + JSON.stringify(mergedCopy));
for (let j=0;j<merged.length;j++) {
  for (let k=0;k<merged.length;k++) {
    if (merged[k]==merged[j]) {
    if (merged[k]==merged[j] && counter>1) {
  counter = 0;
console.log("mergedCopy is now " + mergedCopy);
mergedCopy = JSON.stringify(mergedCopy);
console.log("mergedCopy is now " + mergedCopy);

var splits = mergedCopy.split(",").join("").split('"').join('')
console.log("splits is " + JSON.stringify(splits));
splits = splits.slice(1,splits.length-1);

console.log("splits is now " + JSON.stringify(splits));
var mapped = => parseInt(x));
console.log("mapped is " + JSON.stringify(mapped));
return mapped;

uniteUnique([1, 3, 2], [1, [5]], [2, [4]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:

To start off with, don’t use the JSON.stringify/parse trickery to flatten arrays: you aren’t supposed to flatten anything here - so like JSON.parse(JSON.stringify([1, [2]])) will give you [1,2], which is not what the tests want - look at the result it expects for the one you’re console logging

What you have is overcomplicated; I’ll have a look once I’m at a computer and can test the code, but removing the JSON tricksy stuff is where you can start

Is there something else I can use? Don’t really know another way. Managed to get three challenges right, but then I realized that I was missing the nested arrays.

Basically, just join the arrays together and remove duplicates: the nested arrays are just counted as values themselves, you don’t need to check what they contain.

Note that you can turn the arguments into an array by just doing Array.from(arguments)

And Array.prototype.flat() flattens arrays one level deep; if that isn’t available, you can use Array.prototype.reduce or a loop in combination with Array.prototype.concat()

Note that test doesn’t really make sense - say you have arrays [1, [1]] and [1, [1]] and you run the function. The result should really be [1, [1], [1]] because different arrays are not equal. Which is fine, it just seems pointless, particularly given a union is generally understood to involve merging two (or more) sets of the same thing

Edit: reading up on this may help to massively simplify the code:

Particularly this section:

I am not expert but i can review your code. It’s so tricky. But i can do it.

Let’s go with line by code I got some some array but finally i get the return mapped array as follows

mapped is [1,null,3,null,2,null,null,null,5,null,null,null,null,4,null]

some modification made by me in the code

console.log("-------------------"+  splits);
var splits1= splits.split("");
console.log("splits is now " + JSON.stringify(splits1));

var mapped = => parseInt(x));
console.log("mapped is " + JSON.stringify(mapped));

return mapped;

var v=uniteUnique([1, 3, 2], [1, [5]], [2, [4]]);

By the ways thank you for the post to learn new things… :blush:

------------------------------ Hope this helpful for you.---------------------

--------------------------------------------- Happy coding -----------------------------

Thank you or the info. I tried to use the concat loop. How do I go about removing the unwanted elements from the array? My thought is a double for loop on the array, like before, but as you can see, I have left “” instead of the numbers that appear more than once, which then messes with the square brackets of the nested array.

function uniteUnique(arr) {
  var args = [];
  var mergedArgs=[]

args = Array.from(arguments);
console.log("args is " + JSON.stringify(args));
mergedArgs = args[0].concat(args[1]);
console.log("mergedArgs is " + JSON.stringify(mergedArgs));
for (let i=1;i<args.length;i++) {
  mergedArgs = mergedArgs.concat(args[i]);
console.log("mergedArgs is now " + JSON.stringify(mergedArgs));

uniteUnique([1, 3, 2], [1, [5]], [2, [4]]);