Skip to content

Instantly share code, notes, and snippets.

@alyatwa
Last active September 20, 2023 16:13
Show Gist options
  • Save alyatwa/d1a3282b51c2441ff2f0d1dbc01f75c9 to your computer and use it in GitHub Desktop.
Save alyatwa/d1a3282b51c2441ff2f0d1dbc01f75c9 to your computer and use it in GitHub Desktop.
good array

Good Array Bob doesn't like character arrays having same consecutive character, so he calls them bad arrays. A good array is the one which does not have any same consecutive letters. You are given a character array, make it a good array. The only allowed operation is: If there are two same consecutive letters, delete one of them. INPUT The first line contains an integer T, denoting the number of test cases. Each test case contain an character array. OUTPUT For each test case, print the answer to the given problem. CONSTRAINTS 1 <= T <= 10 1 <= length of character array <= 10^5 Character array consists of only lower case letters Sample Input

3
abb
aaab

Sample Output

ab
ab
ababa

Solution

function main(iterable){
    if(iterable.length == 1){
        return ''
    }
  var unique = []

  for(var i=0; i< iterable.length; i++){
    if(unique.length < 1){
      unique.push(iterable[i])
    } else if(iterable[i] !== unique[unique.length - 1]) {
      unique.push(iterable[i])
    }
  }

  return unique.join('')
}
console.log(main('3'))
console.log(main('aaab'))
console.log(main('abab'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment