title: How do I recursively remove consecutive duplicate elements from an array? (javascript)
body: <p>I am attempting to create a function that aims to eliminate any consecutive duplicate elements in an array recursively. It works with a global variable, however, I find that rather weak work around. I have based my code off of this <a href="https://www.geeksforgeeks.org/remove-consecutive-duplicates-string/" rel="nofollow noreferrer">code</a> (Remove all consecutive duplicates from the string; language used: C++). I understand there are mutability differences between strings and arrays, but I don't exactly understand what is occurring to the stack in the background. Once the function is run, the global variable is correct, but the output from the function itself is not. Any explanation or direction would be greatly appreciated. Thank you!</p>
  
  <p>This is not a homework question, I'm just trying to hammer recursion into my skull as it still throws me for a loop. Sorry for the pun. </p>
  
  <pre><code>//var testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1]
  //compress(testArr); //[1,2,3,1] //&lt;= expected result
  //current output [1, 2, 2, 3, 3, 1, 1, 1]
  var arr = [];
  var compress = function(list) {
       //var arr = [];
      if (list.length === 0) {
          return arr;
      } 
      if (list.length === 1) {
          arr.push(list[0]);
          return list
      }
      if (list.length &gt; 1 &amp;&amp; list[0] !== list[1]) {
          arr.push(list[0])
          compress(list.slice(1,));
      }
      if (list.length &gt; 1 &amp;&amp; list[0] === list[1]) {
          list.splice(0,1);
          compress(list);
      }
      return list;
  }
  </code></pre>