title: Union function - Passing argument in custom function - Google-spreadsheet
body: <p>While trying to answer 
  <a href="https://stackoverflow.com/questions/10793321/how-to-union-range-in-google-spreadsheet">this question</a>, I found a strange behavior.</p>
  
  <p>Here's my code :</p>
  
  <pre><code>function remove(val, array){
    var res = new Array();
    for(var i=0; i&lt;array.length; i++){
      if(array[i] != val){
        res.push(array[i]);
      }
    }
    return res;
  }
  
  //we assume that there is no duplicates values inside array1 and array2
  function my_union(array1, array2){
    var longuer;
    var shorter;
    var arrayRes = new Array();
    if(array1.length &lt; array2.length){
      longuer = array2;
      shorter = array1;
    } else {
      longuer = array1;
      shorter = array2;
    }
  
    for(var i=0; i&lt;longuer.length; i++){
      arrayRes.push(longuer[i]);
      shorter = remove(longuer[i], shorter);
    }
  
    for(var i=0; i&lt;shorter.length; i++){
      arrayRes.push(shorter[i]);
    }
  
    return arrayRes;
  }
  
  function test(){
    Browser.msgBox(my_union([1,2,3], [1,2]));
  }
  </code></pre>
  
  <p>The message box clearly says 1,2,3 but when you try to invoke this function inside a spreadsheet with the same values, it fails to delete duplicated values, why ?</p>
  
  <p>**EDIT : **
  Thanks to Henrique's answer , here's the code :</p>
  
  <pre><code>function remove(val, array){
    var res = new Array();
    for(var i=0; i&lt;array.length; i++){
      if(array[i] != val){
        res.push(array[i]);
      }
    }
    return res;
  }
  
  function matFlattener(matrix){
    var array = new Array();
    for(var i=0; i&lt;matrix.length; i++){
      for(var j=0; j&lt;matrix[i].length; j++){
        array.push(matrix[i][j]);
      }
    }
    return array;
  }
  
  function my_union(matrix1, matrix2){
    //assert no duplicate values in matrix1 and matrix2
    var longuer;
    var shorter;
    var array1 = matFlattener(matrix1);
    var array2 = matFlattener(matrix2);
    var arrayRes = new Array();
    if(array1.length &lt; array2.length){
      longuer = array2;
      shorter = array1;
    } else {
      longuer = array1;
      shorter = array2;
    }
  
    for(var i=0; i&lt;longuer.length; i++){
      arrayRes.push([longuer[i]]);
      shorter = remove(longuer[i], shorter);
    }
  
    for(var i=0; i&lt;shorter.length; i++){
      arrayRes.push([shorter[i]]);
    }
  
    return arrayRes;
  }
  </code></pre>