let str = 'abbaca' function reaa (str) { let arr = [] let strArr = str.split('') for(let index = 0 ;index<strArr.length;index++){ if(strArr[index] == strArr[index+1]){ arr.push(...strArr.splice(index+2)) return reaa(arr.join('')) }else{ arr.push(strArr[index]) if(arr.length ==strArr.length){ return arr } } } } console.log(reaa (str))