0

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.

2
  • Try only working with one copy of the data. Trying to compare two copies and keeping their indices straight is overcomplicating the issue. All you need to track is the current value and either the next or previous value. If current value equals either of those, then discard one because it's a duplicate. As an alternative, you could look into regular expressions. They can do this type of replacement in one line, and if you start using regular expressions now, you'll find uses for them forever. Commented Oct 7, 2016 at 3:58
  • @LinuxDisciple Hi, the idea of tracking the next or previous value came to mind, but I can't come up with a code for that. I'll try it a few more times, and if I still don't get it, then I'll reach out to you. Thank You. Commented Oct 7, 2016 at 4:07

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

If the first character is unique, how does it get into unique? i is never 0...
you are right! so lets fix by add the first temp to unique array I think?
That would work, but then isn't temp always equal to the last value in unique? Do we need temp at all?
for loop will not fail because unique.length is dynamic, i.e will change with addition of new elements to array.
Yes, that. I added an extra line of code, that would unique.push(iterable[0]) before the for loop.
|
1

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
}

Comments

0

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/

1 Comment

I edited @K.Angel7 answer. Added, unique.push(iterable[0]).
0
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

1 Comment

I am supposed to return an array. Although, I've got the correct solution, but I thought I should point it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.