Sorted Union......What is wrong with my code

Hi this is my code for sorted union

function uniteUnique(arr) {
var totalArray=[];
var unique=[];
for (i=0; i<arguments.length; i++){

if(unique.indexOf(value) === -1){
console.log(“unique =”+unique.length);
return unique;

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

thanks in advance

Hi @Bala_Chandra

Your logic is fine, the problem is with this line:


The concat method actually returns a new array made up of the original array and the provided arguments:

Because of this, your not modifying the totalArray and therefore it is empty when you call forEach on it.

Quick fix is to just assign the value from the concat method back to the totalArray

totalArray = totalArray.concat(arguments[i]);

Oh i understood now thank you so much for the reply.


1 Like