relentless-coder
relentless-coder

Reputation: 1546

Need to filter out repeating consecutive characters in a string using JavaScript

It is one of the challenges in Codewars, and I am supposed to write a function that will take a string and return an array, in which I can't have two consecutive identical elements. Also, the order should not change.

For example, if I pass a string "hhhhheeeelllloooooohhheeeyyy", then the function should return an array = ["h","e","l","o","h","e","y"].

This is my code.

    var uniqueInOrder=function(iterable){
  //your code here - remember iterable can be a string or an array
     var unique = [];
   for( var i = 0; i < iterable.length; i++) {
      unique.push(iterable[i]);
   } 

   for( var j = 0, k = 1; j < unique.length; j++, k = j + 1 ){
      if(unique[j] === unique[k]){
         unique.splice(k,1);
     }
    }
   return unique;
 }

so, if I pass a string, such as "hhhhheeeeeellllloooo",it doesn't work as I intend it to because the value of j keeps incrementing, hence I can't filter out all the identical elements.

I tried tweaking the logic, such that whenever the unique[j] === unique[k] the value of j would become zero, and if that's not the case, then things would continue as they are supposed to do.

This got me an infinite loop.

I need your help.

Upvotes: 0

Views: 1085

Answers (4)

DragoRaptor
DragoRaptor

Reputation: 735

var str = "hhhhheeeelllloooooohhheeeyyy";
var strLen = str.length;
var newStr = "";
for(var i=0; i < strLen; i++ ){
    var chr$ = str.charAt(i);  
    //if(i==0) {newStr = chr$ };
    if(chr$ == str.charAt(i+1)){
        strLen = str.length;`enter code here`
    }else{
        newStr = newStr + chr$ ;
    }
}
//document.write(newStr);
console.log(newStr);
//Answer: helohey

Upvotes: 0

KAngel7
KAngel7

Reputation: 161

The second for loop is fail because unique.length is not constant during the run. I think your problem can be solved like this:

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

Hope it helps!

Upvotes: 1

Rohith K N
Rohith K N

Reputation: 871

I think this will help you:

var word="hhhhheeeelllloooooohhheeeyyy"
function doit(iterable){
  var unique = []
  unique[0]=iterable[0]
  for(var i=1; i< iterable.length; i++){
     if(iterable[i] !== unique[unique.length - 1]) {
      unique.push(iterable[i])
    }
  }
  return unique
}
alert(doit(word))

for loop will not fail because unique.length is dynamic, i.e will change with addition of new elements to array.

Tested in Internet Explorer too. Here is the link to jsfiddle: https://jsfiddle.net/kannanore/z5gbee55/

Upvotes: 0

Angrysheep
Angrysheep

Reputation: 422

You only need to compare the current index of iterable against the last character in unique:

function(iterable){
  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
}

Upvotes: 1

Related Questions