title: Recursively remove all adjacent duplicate numbers in an array
body: <p>I want to recursively remove all adjacent duplicate numbers in an array</p>
  
  <p>I have gone through similar links where they did this on strings </p>
  
  <p><a href="https://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/</a></p>
  
  <p>Below is the code for removing adjacent duplicates in a string, I was wondering if there is an ideal way to go along the same lines but run it over an array</p>
  
  <pre><code>  static String removeUtil(String str, char last_removed) 
    { 
           // If length of string is 1 or 0  
           if (str.length() == 0 || str.length() == 1) 
               return str; 
  
           // Remove leftmost same characters and recur for remaining   
           // string  
           if (str.charAt(0) == str.charAt(1)) 
           { 
               last_removed = str.charAt(0); 
               while (str.length() &gt; 1 &amp;&amp; str.charAt(0) == str.charAt(1)) 
                     str = str.substring(1, str.length()); 
               str = str.substring(1, str.length()); 
               return removeUtil(str, last_removed);  
           } 
  
           // At this point, the first character is definiotely different   
           // from its adjacent. Ignore first character and recursively   
           // remove characters from remaining string  
           String rem_str = removeUtil(str.substring(1,str.length()), last_removed); 
  
           // Check if the first character of the rem_string matches with   
           // the first character of the original string 
           if (rem_str.length() != 0 &amp;&amp; rem_str.charAt(0) == str.charAt(0)) 
           { 
              last_removed = str.charAt(0); 
              return rem_str.substring(1,rem_str.length()); // Remove first character 
           }  
  
  
           // If remaining string becomes empty and last removed character  
           // is same as first character of original string. This is needed  
           // for a string like "acbbcddc"  
           if (rem_str.length() == 0 &amp;&amp; last_removed == str.charAt(0)) 
               return rem_str; 
  
           // If the two first characters of str and rem_str don't match,   
           // append first character of str before the first character of  
           // rem_str 
           return (str.charAt(0) + rem_str); 
    } 
  </code></pre>
  
  <p>Suppose if the input array is</p>
  
  <p>1) [2,3,3] - output is [2]</p>
  
  <p>2) [1,2,3,3,2] - [1,2,2] - output is [1]</p>
  
  <p>3) [2, 0, 0, 2, 3, 3, 0, 0, 1, 1] - output is []</p>
  
  <p>Edit - If anyone is still looking for a solution, i figured one way out. I fixed the bug in @kemalturgul 's solution. This was working for me.</p>
  
  <pre><code>public static int[] removeUtil(int[] arr) 
  {
      int i=0;
      boolean check = false;
  
      for (i = 0; i &lt; arr.length - 1; i++) 
      {
          if (arr[i] == arr[i + 1]) 
          {
              check = true;
              break;
          }
      }
  
      if(check)
          return removeUtil(combineTwoArray(Arrays.copyOfRange(arr, 0, i), Arrays.copyOfRange(arr, i + 2, arr.length)));
      else
          return arr;
  
  }
  
  public static int[] combineTwoArray(int[] arr1, int[] arr2) {
      int[] newArr = Arrays.copyOf(arr1, arr1.length + arr2.length);
      for (int j = 0; j &lt; arr2.length; j++) 
          newArr[arr1.length + j] = arr2[j];
  
      return newArr;
  }
  </code></pre>