$9.46

How to divide a string into an array so that each element contains separate duplicate symbols.

Ask a question+
2

How to divide a string into an array so that each element contains separate duplicate symbols.
input aaabbc ,output [“aaa”,”bb”,”c”]

1

Your question is not clear. What output do you expect from “aaabbcaa”? ([“aaa”, “bb”, “c”, “aa”] or [“aaaaa”, “bb”, “c”])

darbinyanraf
2

[“aaa”, “bb”, “c”, “aa”]

Gohar Ghukasyan
add comment

1 Answer

1

I will write it in semi sudo code for you.

var str = 'aaabbcaa';
var temp = str.charAt(0);
var yourarray[];
var searching = true;
var arrayTemp = '';
for (var i = 0; i < str.length; i++) {

   if(!str.charAt(i) == temp){
      --sudo code: push arrayTemp into your array
      arrayTemp= '';
   }else{
    arrayTemp += str.charAt(i);

   }

  temp = str.charAt(i);
}

--sudo code: push arrayTemp into your array (after this for loop)

Basically, check if the letter is same as letter before, if so keep adding to some temporary string, if not, put that temporary string into your array. This will turn aaabbcaa to [“aaa”, “bb”, “c”, “aa”] as requested

Lainrawr 215
add comment

Your Answer